Here's a short guide to set up a Node.js project:
1. Install Node.js
· Download and install Node.js from nodejs.org.
2. Create a Project Directory
mkdir my-node-project
cd my-node-project
3. Initialize the Project
npm init -y
This creates a package.json file with default settings.
4. Install Dependencies
Install necessary packages (e.g., express):
npm install express
5. Create the Entry File
Create an index.js file:
touch index.js
6. Write a Simple Server
Add the following to index.js:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello, World!');
});
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
7. Run the Project
Start the server:
node index.js
Run the server with:
bash
npm start