DEV Community

Sumit Dhanania
Sumit Dhanania

Posted on

Creating a node server

This post will walk you through how to setup your own node server

I am assuming you already have nodejs installed on your local machine. If its not, please download and install it from this link
Installation is pretty straight forward, just follow the onscreen instructions and you are good to go.

There are some core modules that ships with nodejs which are available for us to use it globally (not by default though, we need to import it first and then we can start using it). Here are some of them

  • http
  • https
  • fs

Here, we will be using the http module that will help us creating our own server.

To use the http module, in your js file (lets say server.js) we first need to import it.
We import the module using the require keyword, and store it in a const named http.

const http = require('http');
Enter fullscreen mode Exit fullscreen mode

Now when we have the http module imported on our file, we are ready to use the feature this module offer and start creating our server.

The const http is an object which has certain fields and methods for us to use.
One such method is createServer
As the name suggests, this is a crucial method when it comes to creating a server.

lets call this method and store it in a const named server

const server = http.createServer();
Enter fullscreen mode Exit fullscreen mode

the createServer method takes a function as an argument, so a requestListener which will be executed for every incoming request on our server

requestListener has to receive two argumnets

  • request
  • response

so in short, nodejs automatically gives us some objects which contains the incoming message (the request) and allows us to read data from the request, and it gives us an object response which we can use to send back the response to whoever has sent us the request

This is how the final createServer method looks like

const server = http.createServer((request, response) => {
console.log(request);
});
Enter fullscreen mode Exit fullscreen mode

we are using the ES6 features here

The alternate to this will be to create a separate function and pass the reference to the createServer method.

function requestListener(request, response) {
//do Something
console.log(request);
}

const server = http.createServer(requestListener);
Enter fullscreen mode Exit fullscreen mode

Now, we have our server ready and to use it we need to start listening to it.
This will basically start the server

server.listen()
Enter fullscreen mode Exit fullscreen mode

listen takes a couple of arguments, optional arguments
the first one is the port, now in production you will not be giving the port, it automatically takes up default 80

Since we are running on our local machine, we will give the port as 3000 (you can give any port of your choice)

you can also define a hostname, which gets passed as a second argument. But since we are running it on our local machine, this is localhost by default

server.listen(3000);
Enter fullscreen mode Exit fullscreen mode

now, when we run this script

node server.js
Enter fullscreen mode Exit fullscreen mode

here you will see that, nodejs will not immediately exit our script, but it will keep on running to listen the requests we are getting on the server (thats the main purpose of server.listen() )

Now when you hit localhost:3000 on your browser
you should see the request details being logged in your console.

Alt Text

Now this will keep on listening to requests, and to stop it we need to hit ctrl + c
This will stop the script execution and localhost:3000 will not be accessible anymore.

Thats It!
This is how you create a server in nodejs and I know this can be hard to wrap your head around if you are just starting out with nodejs

If you are someone who is coming from a php background , or someone who has worked on php
this is similar to what xampp does.
It creates a server for us out of the box and gives us access to localhost on which we write our php codes

Here using nodejs, we have built our own localhost .

Top comments (0)