node js

What is node js? Tutorial for beginners.

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.

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 inherits the same. Node Js is available on almost all operating systems.

Node Js Features:

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

It is an open-source platform:

Open source has many advantages over proprietary code. The first advantage is cost. Another significant advantage is, having many developers across the globe to contribute. Thousands of developers have developed and tested it worldwide, and it 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, 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:

Due to this, any request in Node does not wait to be completed before handling another request. This is achieved in Node js with the concept of callback functions. Each API call can specify the callback function when the API completes the callback function is called. This makes the program nonblocking.

If a request is synchronous, the server must complete that request first and then process the subsequent request. This may block other requests to wait. There may be multiple threads for handling requests, but again, resources limit threads.

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 efficiently utilize 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 create 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 platforms, 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

For 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 a local directory, node_modules. If you want the new module to be accessed by other developers on the same machine, use global mode.

[root@CentOS_6_64-160 client]# npm install thread-sleep -g
└── 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, is very easy from the epel repository.

EPEL  repository is available for CentOs and others.

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

yum install epel-release

Now the repository is accessible.

For installation, use the below command.

yum install nodejs

Check if it has been installed successfully. This will get the node version.

node –version


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.
  • The startup menu will have 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 the npm version. Once installed now, let’s develop a basic program using Node Js. 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) {
console.log('Error In reading' + err);
else if(contents)
console.log('File Content = ' + contents);
console.log("No Error and No Content");
console.log('Read File Started');

The first line includes the module required for file handling. This is an 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