node js

ain What is node js or node js tutorial :

Node js is a compelling platform for developing I/O intensive web applications. Most web applications need to handle a very high volume of concurrent requests. Node js tutorial explains what node js and installation of node js on windows and Linux is. The use of javascript was widespread in the web browser for a long time. Node is the framework to run javascript outside a web browser. Javascript is an event-driven language, node js inherent the same. Node Js is available on almost operating systems.

Node Js Features:

Nothing completes without discussing the main features of a platform. This section of the tutorial will cover key features of node js with detailed examples.

It is an open-source platform:

An open-source has many advantages over proprietary code. The first advantage is cost, another major advantage is, having many developers across the globe to contribute.  This means developed and tested by thousands of developers across the globe and will be freely available. The great community of open source developers keeps adding new features to the Node js with no added cost.

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 a new concept, this has been enhanced to work on console-based applications.

Node Js is Asynchronous in nature:

Due to this, any request in Node does 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 makes the program nonblocking.

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

Single-threaded model for Node Js:

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

Node Js applications are written in javascript.

Java script, an already known language, was used to develop client applications, so it is easy for the developer to develop high-performance server applications. The Node is the environment that enables the javascript to execute on the 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,

[[email protected]_6_64-152 /]# npm --version
 -bash: npm: command not found
 [[email protected]_6_64-152 /]#

Not Installed? , install npm

[[email protected]_6_64-152 /]# yum install npm

[[email protected]_6_64-152 /]# npm --version
 1.3.6

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

[[email protected]_6_64-160 ]# npm install thread-sleep

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

[[email protected]_6_64-160 client]# npm install thread-sleep -g
/usr/lib
└── [email protected]

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 another 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 on Windows is easier 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 the installation is done.
  • In the startup menu, there will be an icon for the node js command line.
  • Reboot the machine.

Check if it is installed?

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

var fileModule = require('fs');
fileModule.readFile('example.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 an Asynchronous API call. The output will be

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

 

Leave a Comment