step 1: setup Node.JS Development Server Environment
first thing that if you have not installed node.js app in your computer. then download node.js from https://nodejs.org.
after downloading setup node.js path in your computer directory then open the terminal and run following command to check node.js successfully setup in your computer. if you are seeing version of node on terminal.
- node –v
Step 2: Initialize your app directory
open terminal or command prompt now the time to create your app directory and navigate root directory it using terminal then run the following command for initialize new node.js app
-
npm init -y
this command create package.json file.
Step 3: install Express Module
now time to install Express module and save it in your project dependencies. for installation run the following command.
-
npm install express
Step 4: create server folder directory
inside the src directory, create the main server file, often named server.js or app.js, where you’ll set up your Express server.
Start Coding: Write the code for your server in the server.js or app.js file. This will include requiring the Express module, setting up middleware, defining routes, and starting the server to listen on a port.
in server.js add the following code
const express = require('express');
const app = express();
app.use(express.static('public'));
app.get('/', (req, res) => {
res.send('Hello World!');
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
this code set the port to 3000 that send a welcome msg. start your development server.
Step 5: Lunch your NodeJS server
in terminal navigate your App directory and run the following command
-
node server.js