Browse By

An Absolute Beginner’s Guide To Node.js First Application

The popularity of Node.js has been skyrocketing in the last few years. In fact, it has given birth to numerous Node development companies that have taken Node application development to a whole new level. Node.js is basically JavaScript on the server which has been proven to be effective server side language. The main concept behind node.js is event loop which means everything executes in a single thread and the inbuilt I/O functions execute asynchronously to prevent blocking the loop.

An Absolute Beginner's Guide To Node.js First Application

In today’s post, we are going to create a simple application with Node.js to introduce various Node.js concepts.

Web servers and threads

Let’s understand web servers and threads with the help of PHP. When you create web applications and sites with PHP, you connect the language with an HTTP server like Nginx or Apache. Both the language and web server have their own roles to play in the process.

The language i.e. PHP executes the .php file code and forwards the result to the web server i.e. Apache, which then displays the results to the user.

Apache, the web server, manages the HTTP requests to connect to the server. The major role of the web server is to manage the ins and outs of the traffic.

Threads are basically the same page requests originated by different visitors. The web server is responsible for handlings all the threads and running them simultaneously. Every thread generated by the visitor uses a processor code or a different processor on the server.

However, Node.js does not use any HTTP server like Nginx or Apache. It all depends on the user to create the server.

Also, Node.js is monothread unlike Apache, which means the server can handle and run only one thread at a time and there are only one version and one process of the program that can be executed. Despite the fact that it is monothread, it performs each task efficiently and much faster, thanks to event-oriented nature of the language.

Without further ado, lets start creating our first application with Node.js.

Step 1: Import required module

In order to load HTTP module and save returned HTTP instance into the respective variable, you need to use “require” directive
For example:
var http = require(“http”);

Step 2: Creating server

Once you have received http instance, use it and call http.createServer() method to build server instance. Now, use listen method associated with server instance to connect it at port 8081. Use a function with parameters response and request.

Use the following example to get “My first Node application”.

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 “My First Node Application”
response.end(‘My first Node application\n’); }).listen(8081);

Console will print the message
console.log(‘Server running at’);
The above code will create a HTTP server which responds. Thus, you need to wait on local system for a request over 8081 port.

Step 3. Testing request and response

Now, you need to create a file “main.js” to put step 1 and step 2 together and begin your HTTP server as follows.

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 “My First Node Application”
response.end(‘My First Node Application\n’);
// Console will print the message
console.log(‘Server running at’);
Now run main.js to begin the server as shown below:
$ node main.js
Verify the output and Server has begun
Server running at

Step 4. Make a request to Node.js server

Once verified the output. Open in your browser to get the desired result i.e. “My First Node Application”

About Author:

Marie Thomas is a renowned web development professional who works with WordSuccor Ltd., a leading WordPress Website Development Company with a global reach. She is a passionate blogger who loves to write and share everything about WordPress and new web design technologies. For those who are looking to hire WordPress developers that can match pace with the competitiveness can count on Marie for it.

One thought on “An Absolute Beginner’s Guide To Node.js First Application”

  1. Latest Government Jobs 2016 says:

    Amazing Information in the post

Leave a Reply

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