Node.js Simple Application

If you are reading this, it means you have a theoretical understanding of Node.js and you have installed local node.js environment. However, if you haven’t installed a local environment, I would recommend you to read the last article. In this article we are going to develop a node.js simple application.

Major Components of a Node.js Application

There are three major components of any node.js application.

1) Importing Modules

Node.js comes with variety of built-in modules which contain different functionalities. In order to use that functionality, you need to import the module first. In node.js “required” command is used to import the desired modules.

2) Creating a Server

The second major component of a node.js application is a server which listens to requests on a specific port. Server receives request from the client, processes it and returns the response.

Sending a Request

The third and final component of a node.js application is the request to the server. Request can be sent to a server via console client or web browser.

Node.js Simple Application

In the upcoming section, we shall create a simple node.js application that will contain a server and it will respond to the requests generated by clients. Take a look at the following code.

var http = require("http");

http.createServer(function (request, response) {

   response.writeHead(200, {'Content-Type': 'text/plain'});
   // Send the response body as "Welcome to knowledge hills"
   response.end('Welcome to knowledge hills\n');

// Console will print the message
console.log('Server listening for requests at');

Download the code

Now take a careful look at the above code. It contains a line “var http = require(“http”);”. Here we are importing the required “http” module. This “require” call returns an HTTP instance which is stored in the “http” variable.

Next we call the createServer function on the http instance. This creates another function as a parameter which implements the request and response model. At the end of the createServer() method call we append the listen(9006) which specifies the port on which the server will listen.

We know that node.js applicationsa are non-blocking, therefore once we call the createServer() method and it starts listening for the requests, the control shifts to the next line of code where we can execute anything. In the above code we simply display a message via console.log.

Now inside the function, we send response when client requests is received. In response we end the header whose first parameter is 200 which means that status is okay. Next we specify the type of content that the server will send. And finally we send he response.end() method to send the response body.

To execute the above code, simply execute the above application via command prompt. A message shall appear on command prompt that ‘Server listening for requests at’. This means that server is successfully running. Now open your browser and type this address “”. On the web page that opens, you shall see the following message “Welcome to knowledge hills”. Hence, this is how we create node.js simple application. Happy Coding!

<<< Node.JS Environment SetupNode.JS REPL Terminal >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .