node js

What is node js or node js tutorial :

Node Js is a very powerful platform for developing I/O intensive web applications. Most of the web applications needs to handle very high volume of concurrent requests. Node js tutorial explains the core concepts. Use of java script was popular in web browser from long time. Node is the framework to run java script outside the web browser. As java script is the event driven language , it inherent the same.

It is an open source platform,

This have advantage of developing and tested by thousand of developers across glob and will be freely available.

Extremely fast: It is built on Google Chrome's V8 JavaScript Engine. Which very optimized for execution. This search engine is already well tested for a very high volume. For new concept this has been enhanced to work on console based application.

Node Js is Asynchronous in nature. Due to this any request in Node do not wait to complete before handling another request. This is achieved in Node js with the concept of call back functions. Each API call can specify the callback function, when the API completes the call back function is called. This make the program to non blocking.

if a request is synchronous, the server has to complete that request first then go to process next request. This may block other request to wait. There may be multiple threads  for handling requests which, but again threads are limited by resources.

Single threaded :

As node js do not have threads , this make the server to handle very high volume of incoming requests. As most of the servers are limited by the number of threads or resources. But this does not utilized efficiently multiple cores of a CPU.

Node Js applications are written in java script.

Java script a already know language, it was used to develop client the applications, so its easy for the developer to develop high performance server applications. The node is the environment which enables the java script to execute on server side. Node Js can run on various platform e.g Linux, Windows and Mac OS.

Node Js is module based,

A module in node is kind of library written in java script. Library having developed and tested functions. Using build in functions saves time and provide rapid development for new features. To get access to a module , node js program has to include that module.
E.g to write a web server a http module is required.
var webserver = require('http');
There is community that continuously developing new modules. This enables a very rapid addition of new modules.

How to install a node js module or NPM:

A module can be installed from npm or Node Package Manager.  First the npm itself should be installed.

Check if the npm is installed ,

[root@CentOS_6_64-152 /]# npm --version
 -bash: npm: command not found
 [root@CentOS_6_64-152 /]#

Not Installed? , install npm

[root@CentOS_6_64-152 /]# yum install npm

[root@CentOS_6_64-152 /]# npm --version
 1.3.6

Example , if you want to use sleep in a program. Sleep is in module thread-sleep.

[root@CentOS_6_64-160 ]# npm install thread-sleep

This will install the module in local directory , node_modules. If you want this to accessible to other developers also on same machine then use global mode.

[root@CentOS_6_64-160 client]# npm install thread-sleep -g
/usr/lib
└── thread-sleep@2.0.0

The module is installed in /usr/lib/node_modules.

Node Js Installation on linux:

It can be installed from various methods , very easy is from epel repository.

EPEL is repository is available for centos and other os.

epel-release should be installed to get access to epel.

yum install epel-release

Now epel repository is accessible.

To install use below command.

yum install nodejs

Check if it has been installed, this will get the node version..

node --version

v0.10.30

npm can be installed now as well

yum install npm

Installation on windows :

Installing  windows is easy than on linux, as it provides the graphical interface for installation. For installation first download the installer from  Node Js Installer .

  • Click to install.
  • Click next and accept the agreement and click next. You will see the progress bar for installation.
  • Click finish once installation is done.
  • In startup menu there will be icon for node js command line.
  • Reboot the machine.

Check if it is installed?

Type node -v on cmd , it will print the node js version.  To check the NPM , type npm -v , it will show npm version. Once installed now developing a node js basic program.  It reads a file helloFile.txt and prints the content on console.

var fileModule = require('fs');
fileModule.readFile('helloFile.txt', 'utf8', function(err, contents) {
if(err)
{
console.log('Error In reading' + err);
}
else if(contents)
{
console.log('File Content = ' + contents);
}
else
{
console.log("No Error and No Content");
}
});
console.log('Read File Started');

The first line includes the module required for file handling. This example of Asynchronous API call. The out put is

[nodeExample]# node hello.js
Read File Started
File Content = My First Programe --

 

Leave a Comment

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