Node.js Tutorial

Node.js enables Javascript to run in back end. Traditional usage of Javascript was in client side scripting. Node.js is an open source javascript run time environment which is using to develop  applications. Node.js  interprets Javascript with Googles V8 VM. Node.js ships with some useful modules.That means  node.js has a run time and a library.So developers need not develop everything from scratch . Node.js applications can be written in any language which can be compiled to Javascript.

History of Node.js

Node.js was initially developed by Ryan Dahl in 2009.Now the development is sponsored by  Joyent. In 2010 Node Package Manager was introduced for managing libraries.In 2011 ,Windows version of Node.js was released by Microsoft and Joyent. Currently , a large number of  packages and utilities are there in npm repository.

Introduction to V8 VM

V8 is Google’s open source Javascript engine written in C++.It is used in Node.js to compile Javascript code to native  code .V8 is mainly  used in Google chrome ,Chromium and in some other browsers.It can run in any platform.Also it can be integrated with  any C++ application.It can run stand alone as well.It follows the ECMA-262. V8 is faster than other javascript engines like Spider Monkey.That could be the reason to use  this one for Node.js development.

Node.js Architecture

Node.js enables the developers to develop web servers without threading.This is accomplished by event driven programming that use call backs to complete the task.In web servers like Apache HTTP ,a separate thread is created for each incoming client requests.But for node.js servers , it has only one thread for all the incoming client requests.This is the asynchronous event-driven main thread.The main thread posts the independent tasks to a task queue. Node.js has a libuv library which has a thread pool with a fixed number of threads.Those tasks would be taken up by the threads in the thread pool.When a task has completed,it informs the main thread and hence the registered callback would be executed.And thus the response would be sent back to the client.

node-js-tutorialNPM

It is the Node Package Manager.Node.js has an npm registry ,which has a lot of packages. NPM is used to install and manage those packages from the registry.

Installing a Package using npm

npm install <package-name>

Uninstalling  a package

npm uninstall <package-name>

Global Vs Local Installations using NPM

Using npm , a package can either be installed locally or globally. Default installation is the local installation.There ,the installation happens in the node_modues directory of the application/module.Whereas in global package installation,installation happens in system directory.

A global package installation can be done like

npm intsll <package-name> -g

Installing Node.js in Fedora Linux

1)Install.js and npm.I am currently using Fedora24,so dnf command will do the installation for me.Prior to Fedora 22 ,we should use yum command.

  $ sudo dnf install nodejs npm

2)To compile and install native addons from npm ,we may need to install build tools

 $ sudo dnf install gcc-c++ make

Its all about the installation in Fedora.One can verify the installation by typing the commands.

Installing Node.js in Windows

Download and install Node.js from here.

Node.js example

Let us design a Node.js server which returns a string message to the requesting clients.

1)Create a project directory

$ mkdir nodesample

2)Go to the directory and create a sample.js file.Open the sample.js file

 $ cd nodesample/
 $ vi sample.js

Paste the following contents  to the sample.js file.

var http = require('http');
var server = http.createServer(function(request,response){
console.log('New client request came');
//send http status as 200 OK
response.writeHead(200, {'Content-Type': 'text/plain'});

// Send the response message
response.end('Welcome to the world of Node.js');

});
server.listen("4455");
console.log('server is up on 4455..')

3)Run the sample.js by using the node command

$ node sample.js

The  message ‘server is up on 4455..’  would be displayed in console which indicates that the server is running and ready to accept requests from clients.

4)Now open the browser and go to the url : http://localhost:4455.The message would be displayed in the browser window.nodejs-tutorial-exampleCreating a Node.js Package

It is very easy to create an npm package or module locally and use it .We are planning to create a module ‘samplemodule’  which displays a message in the console(To make it simple ).Then we can install and use the package.Once it is installed locally or globally,we can call the package and use.

1)Create a directory

$ mkdir SampleModule

2)Go to the new directory

$ cd SampleModule/

3)Run command

  $ npm init

This would prompt the us to enter the details.Once all the details were entered a package.json would be created.

Significance of package.json
The package.json contains the module metadata.This file gives inforamation to npm about the name , dependancies etc.

This case the package.json generated would be:

{
"name": "samplemodule",
"version": "1.0.0",
"description": "This is my first Node.js module",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Bijoy",
"license": "ISC"
}

4)Now create the file to be loaded when the module is requested.Since we opted index.js while doing the npm init command,create the index.js file in the module.

  $ vi index.js

and just add a function to the exports object which simply displays any message in the console.

exports.sampleFunction = function() {
console.log("Message from samplemodule");
}

5)Install the module:npm install -g <module-name> (with root user ,if needed)

$ npm install . -g

in the sampleModule directory .So we installed it globally.

6)Once it is installed,just create another test file out side our package .Open it and call the package to invoke the function too see the message in the console.

 $ vi testsample.js
var sampleModule=require('/usr/lib/node_modules/samplemodule');
sampleModule.sampleFunction();

7) Now run the testfile.

 $ node testsample.js.

The message “Message from samplemodule” would be displayed in console.

Advantages of Node.js

  • Since Node.js uses javascript as back end ,it is fast
  • Since Node.js is event driven,it is suitable for real time applications like chat applications.
  • Good number of packages in the NPM repository gives an edge to the developers

Limitations of Node.js 

  • No support for multi threading
  • It is not suitable for heavy computational tasks.