How to create Server

To create the server we need some packages

1) firstly install express package


npm i express –save

2) after that install http package


npm i http –save

Now, create the file server.js and put the below code


var http = require('http');
var express= require('express');
var app=express();
const httpServer = http.createServer(app);
httpServer.listen(4444,() => {
    console.log('HTTP Server running on port 4444');
});

Note:- 4444 is a port number where Nodejs will run and you can create any port number according to you.

//run the file
node filename


node server.js
Output:- http://localhost:4444

HTTP Server running on port 4444

Note:- where require method is use to include the module.

How to create Server – Interview Questions

Q 1: How do you create a basic server in Node.js?
Ans: A basic server can be created using the built-in http module and the createServer() method.
Q 2: Which module is used to create a web server in Node.js?
Ans: The http module is used to create a web server.
Q 3: What does listen() method do?
Ans: The listen() method binds the server to a specific port and starts listening for incoming requests.
Q 4: What is Express.js?
Ans: Express.js is a lightweight Node.js framework that simplifies server creation and routing.
Q 5: Why is Express preferred over the http module?
Ans: Express provides middleware support, easier routing, better error handling, and faster development.

Related How to create Server Topics