Initialize a new Node.js project: This will create a package.json
file in your project directory.
mkdir my-node-project
cd my-node-project
npm init -y
Install dependencies: Use npm
to install any libraries you need. For example, if you need Express.js, you would run:
npm install express
Create your main file: Typically, this is index.js
or app.js
. Here's a simple Express server
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
Run your application: You can start your Node.js application with the following command:
node index.js
Remember to replace express
with any other dependencies you might need for your project. Also, you can add scripts to your package.json
to streamline the start-up process, like so:
"scripts": {
"start": "node index.js"
}
Now, you can start your server with npm start
. This is a basic setup, and your actual implementation might vary based on the project requirements.
Top comments (0)