Creating a node server
Tags: node
Published: 2020-11-22 10:26:21

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](https://nodejs.org/en/)
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.

```javascript
const http = require('http');
 ```

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

```javascript
const server = http.createServer();
```

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

```javascript
const server = http.createServer((request, response) => {
console.log(request);
});
```

> we are using the [ES6](https://github.com/lukehoban/es6features) features here

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

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

const server = http.createServer(requestListener);
```

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

```javascript
server.listen()
```

`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

```javascript
server.listen(3000);
```

now, when we run this script
```javascript
node server.js
```

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](https://dev-to-uploads.s3.amazonaws.com/i/o6kg1pw75096d9dguvjp.png)

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` .