Unseeing possibilities through Node.JS



What is Node.JS?

Node.JS is a server side event driven I/O to build fast, scalable network applications. To know more about Node.JS you can get more information at http://en.wikipedia.org/wiki/Nodejs . In this blog I am more focused to talk on the practical done by me.

Node.Js is all about:

1.     Asynchronous I/O Framework

2.     Core in C++ on top of V8

3.     Rest of it in Javascript

4.     Can handle thousands of concurrent connections with minimal overhead (CPU/Memory) on a single process

5.     Works upon v8 which is a potent platform developed by Google in C language. Node.JS sits above V8 and uses the APIs of V8

Once you download the Visual SDK toolkit for Node.JS you can verify the same with node command. Following sample shows how to write a simple addition and multiplication function and call them


Similarly you tend to do some management related operations like monitoring the event Eventemitters by calling the process command.

Let’s start with some real world application, like pinging a URL and getting its status code. So how about writing a Javascript code that will do a http GET request to http://www.google.com/ and respond us with the status code

First of all you need to download a module which we will use to do the http request. The module name is request.

In your Node Command shell you need to type the following

Ø Npm install request

Here npm means node package manager. This command instructs the package manager to initiate. “Install” gives the instruction what has to be done, in our case we need to install a module and “request” is the module name that we intend to install.

Once we execute this it will try to get the module from registry.npm.js.org


Now we are ready to script. The following script is written to do a http request to google.com and return the status code to the user

Getrequest.js

var request = require('request');

request.get(

            'http://www.google.com/',

            function(error, response, body) {

                        console.log(response.statusCode);

            }

           

            );

In the above code we are calling the request module and then setting the request method to get before requesting http://www.google.com/

Similarly we wrote another script putrequest.js which had the get method changed to put

Putrequest.js

var request = require('request');

request.put(

            'http://www.google.com/',

            function(error, response, body) {

            console.log(response.statusCode);

            });

And a third script which will not return the status code but will return the body of the response.

Putrequestbody.js

var request = require('request');

request.put(

            'http://www.google.com/',

            function(error, response, body) {

            console.log(body);

            });

In the third script the point that I want to make is EventEmmiter. Compare to the above two scripts which were calling response.statusCode which is an eventEmmiter but was returning the statuc code of the request. If I want to log the body content of the request, a change in EventEmmiter will help me do that.

Output
I have documented all the 3 outputs in one console for you to relate. The first script which was to get status code for http://www.google.com returned 200, the second was to verify whether put request is allowed, returned 405. The third returned the body of the error caused by put request on google.com.
The point is, it is very easy to write a http method scanning app through the use of such lightweight Node.JS scripts. Now we are ready to write some code in Node.JS. Very quickly I will be writing the next part of this blog, which will talk about Why Node.JS and what is the role of Node.JS in Azure? What is GIT? and how can we publish our Node.JS through GIT?

Comments

Popular posts from this blog

Firebase authentication with Ionic creator

Big Data - SWOT Analysis

LINKEDIN api call using NODE.JS OAUTH module