The following code shows Creating a Server in NodeJS.

Here we need to use the ‘http’ module for creating the server. Furthermore, use the createServer() method and specify the function to execute in it. After that call the listen() method and specify the port number.

var server=require('http');
server.createServer(function(request, response){
response.write('https://www.programmingempire.com/: A NodeJS Server');
response.end('A Basic Example of NodeJS Server!');
}).listen(3219);

Output

Now run the following command to start the server.

Starting an HTTP server
Starting an HTTP server

Once we start the server, we can open the web page at http://localhost:3219/

The Output of Creating a Server in NodeJS
The Output of Creating a Server in NodeJS

Further Reading

Understanding NodeJS With Examples