Writing the first Nodejs Server
To implement the Node.js Server, we are going to use the HTTP module. To know all about Nodejs modules, visit nodejs.org/api.
Step 1: Import the HTTP module and assign its value to a constant.
Step 2: Declare a const and assign value as 8000. This is the port at which the server will run. The value can vary according to the requirements.

Step 3: Now we will use the http module to create server and store it in a constant. Also while creating server, a function should be passed that can handle the request and response received on the server.

Step 4: Define the function, requestHandler above the createServer method and pass two arguments. In this example, we are using req aka request and res aka response.
The first argument has a method ‘url’ used as req.url which returns the requested URL on the server, on the other hand res has a method ‘end’ used as res.end that is used to serve the content on internet.

Step 5: Finally, we use server.listen method to listen to the server. Two arguments that are passed include port that we have declared above and a function while catching a error. This function will either return an error or print output on the console.

Congratulations! You have now setup your very own nodejs server on your machine.
Now to run the code, open terminal with the folder directory and type the following command:
node index.js
Check out the Full Code below:

Links to our previous post on Nodejs: