BUILD YOUR 1ST APPLICATION | NODE.JS

16 / 100

In the previous blog, you have learned how to install node.js and npm on your system. So that you can start working with the server-side platform. Now I believe that you have understood why you should learn node.js. Now let’s create our first program and understand how does it work.

Node.js consists of three very important components:

  1. Import Require modules – Here the required directive is used to load the Node.js built-in/installed modules.
  2. Create Server – When the client sends a request, a server will listen to it and it is similar to the Apache Server.
  3. Read request and return response – The server which we created in the previous step will read the HTTP request which a client will make over the browser or a console and will return the response.

Now, let’s create our first application step by step and understand its proper working.

CREATING NODE.JS APPLICATION

STEP 1: Import Require Module

As discussed above to load any Node.js HTTP module we use the required directive and store the returned HTTP instance into the variable that we will create. For example:

var http = require("http");

STEP 2: Create Server
Now that we have the http instance that we created in the first step. Now call the http.createServer() method for creating the server instance, after that we’ll bind it at port 8080 with the help of listen method which is associated with the server instance. Pass the function into it with parameters request and response. Use the below code for the implementation of your first application with NOde.js

http.createServer(function (request, response) { 
// Send the HTTP header 
// HTTP Status: 200 : OK 
// Content Type: text/plain 
response.writeHead(200, {'Content-Type': 'text/plain'}); 
// Send the response body as "Hello World" 
response.end('Hello World\n'); 
}).listen(8080); 
// Console will print the message 
console.log('Server running at http://127.0.0.1:8080/');

With the above code you can create an HTTP server which can listens for the requests, like in our case we have created an HTTP server that listens to the requests over 8080 port on the localhost.

STEP 3: Testing Request and Response
Now combine step 1 and step 2 together and save it in a file called main.js and run the HTTP server:

var http = require("http"); 
http.createServer(function (request, response) { 
// Send the HTTP header 
// HTTP Status: 200 : OK 
// Content Type: text/plain 
response.writeHead(200, {'Content-Type': 'text/plain'}); 
// Send the response body as "Hello World" 
response.end('Hello World\n'); 
}).listen(8080); 
// Console will print the message 
console.log('Server running at http://127.0.0.1:8080/');

To execute the above code open the terminal for the same directory where main.js file is and run the given command:

$ node main.js

When you will execute this command you will see the output like this:

Server running at http://127.0.0.1:8080/

Now make a request to the Node.js server by opening the URL you got in the output. Once you will make the request to the created server and open http://127.0.0.1:8080/ you will see the output like shown here:

Congrats, you have created your first application with Node.js with HTTP server up and running which is responding to all your HTTP requests at the port specified 8080.

This is all about creating your first application with node.js, I hope you have gained some good quality of knowledge from here. If you have any query then feel free to leave your query below in the comment section.

Stay tuned and Happy Learning. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *