Writting Our First Node JS Server

Today we will create our own node js server. This will be a very basic server running on a port in our machine, that can be accessed with localhost. This will be a very basic server for beginners. So let’s start.

The first thing that you would need is that node should be installed in your machine. If you do not have node JS installed in your machine you can go to https://nodejs.org/en/ to download and install. It is available for Windows, Mac and Linux. So choose according to your operating system and install the latest version of Node JS.

Now to see whether node JS is properly installed or not, open command promp and type the below command

node --version

If you see a version number like v12.12.0, that means node is successfully installed in your system. You are now good to go. If you see something like “node is not recognized as an internall or external command”, then your node installation is node done properly yet. In most of the cases a simple system restart would to the trick.

Now that you have installed node, the next thing that you need is a code editor, where you can write your code. I would recommend you to go for Visual Studtio code which can be downloaded from https://code.visualstudio.com/download. This is also available for Window, Mac and Linux.

Now that you have all the pre-requisite installed, it’s time to get your hands dirty with code. For that fire up your text-editor, browser and your command prompt. Let’s go step by step from here

1. Create a folder or directory at a location of your choice in your system. Preferably name is as “My First Server”.

2. Create a file called server.js inside that folder. See the js file extention which is a must for writing node JS code.

3. Now open the file with your favourite text editor. You should see an empty file(obviously !).

4. The first thing we need to do is to import and http module. A module is a set of code already written for us and it servers a particular purpose. Here http module is inbuilt with your node js installation. This module is used to deal with http request. And as you know the web is build with http protocol.

const http = require('http');

Here we are creating a JavaScript constant that stores the http module that is given to us by Node.

5. The next step is to create a server with the help of our http constant that we have created in step 4. This server takes one callback function which gives us access to request and response object. We will bind a string “Hello World” to the response object which can be seen in a browser.

const server = http.createServer((req, res) => {
    res.end('Hello World');

As you can see here we are attaching a string “Hello World” to our res object and at the same time we are closing/ending our response stream. If we do not close our response stream then the response will never be sent back and eventually http will time out.

6. The next step is to listen to the server on a port number, which makes our server accessible via http calls. Here we will use port 3001 to listen to the http requests.

server.listen(3001, () => {
    console.log('Server is running on port 3001...!');

Here we are listening to port 3001. When server will start, we will get a console message telling us that the server has started on the given port.

7. The next thing is to go to your command prompt and navigate to the folder where your server.js file exists. In that folder type the following command.

node server.js

If you have used a different file name then use that file name instead of server.js. If your code is correct then it should compile and give you the message that we have added in our console in step 6 i.e., ” Server is running on port 3001…! “.

If you get the message then congratulations, you have successfully create your very first node JS server. But if you get any error message, go back to your code and compare your code with what I have given. See carefully if you have done any typos in your code and try recompiling againg.

8 The last step is to open your favourite browser and hit http://localhost:3001 and you should see “Hello World” displayed in your browser. If you have used any other port then use that port instead of 3001.

That is it. You have successfully create your own Node JS server.

Here is the code that I have explained above. Feel free to use this code if you are stuck and not able to complile your code.

const http = require('http');

const server = http.createServer((req, res) => {
    res.end('Hello World');

server.listen(3001, () => {
    console.log('Server is running on port 3001...!');

Thank you Reader.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: