Wednesday, February 25, 2015

NodeJs

NodeJs

What is NodeJs?

It's a cross-platform RunTime Environment (Java Developers can visualize it like JRE with an exception that NodeJS is utilized to execute JavaScripts) that uses Chrome V8 JavaScript engine to execute the JavaScripts.

Why NodeJS?

JavaScripts are normally interpreted by Browsers. When considering the Environment out of browsers, JavaScripts aren't that powerful.

Javascripts din't have access to File System for years. Though HTML5 introduces File API, the concept still doesn't gain much support outside chrome.

How great would it be if we execute our Javascripts in some platform other than browsers and also impart many features to JavaScript. NodeJs serves this purpose.

When we need to go for NodeJs?
Normally we start using NodeJs at two instance

First is that when we need to work with utilities built on top of NodeJs like Grunt, Gulp, Yeoman. Installation of these Node Package requires NodeJs to be installed in the System. [NodeJs is required here just to install other Node Package and we don't need to be expertise in NodeJs. Just an Installation of NodeJs would do (Node Package Manager comes by default with NodeJs Installation)]

Second is when you deal with Web Application Frameworks like Express, Koa etc that's being built on NodeJs, you obviously would have to deal with NodeJs. [In this case as a NodeJs Engineer, you would be dealing on how to setup the Server, Rest API Calls etc]

How NodeJs Works?

Architechture

NodeJS is built using the Chrome V8 JavaScript Engine and other C/C++ Libraries to support HTTP, TCP, DNS, Asynchronous I/O.

Also composed of JavaScript Modules.

Working with NodeJs -

Sample Execution of Javascript in NodeJs

Let's have a look at the sample tutorial of running JavaScript in NodeJs

Step 1: Make sure NodeJs is installed. Then type in node to enter the console region.

var a =1;

console.log(a);

Step 2: Ctrl + C twice to exit the node console.

Step 3: We can also prepare a separate javascript file with the contents and execute it as follows with the contents of file being. (Note: Make sure you are in the directory where the javascript file is in. Here the javascript file is considered as Module by NodeJs)

var a =1;

console.log(a);

Sample Execution of calling Module from another Module:

Step 1: Create a file named sample.js with contents

var a=1;

console.log(a +'There');

module.exports.a =a;

Step2 : Create a file named sample2.js with contents

var sample = require('./sample.js');

console.log(sample.a);

Step3: Execute the Module sample2.js. The result is that contents of sample.js gets executed at first and also export the contents of module 1(sample.js) to module 2(sample2.js). [ Note : module.exports.a =a; at sample.js file would save the value of a in exports module typed a. We inturn acquire this in sample2.js file using the variable named a which was earlied given to it]

Node Package Manager

Node Package Manager comes by default with NodeJs and helps in downloading, installing and managing the Node Packages.

Based on the knowledge on modules, lets' try to install a module named underscore.

Let's setup the directory named Test and place the sample.js and sample2.js created as above within Test directory . Now navigate to directory Test and run the command npm install underscore

[ Note: We can also do npm install underscore -g where this would install the module in global directory C:\Users\Istherino\AppData\Roaming\npm\node_modules\ at which the module could be shared by all the Projects ]

This would download the underscore module / node package and place it within node_modules directory [node_modules directory is created in a place where the npm install underscroe command is executed]

Let's try to refer the downloaded module from sample2.js. Include the import module statement ( var underscores = require('underscore')) in sample2.js as below such that sample2.js file looks as below. This would look into the node_modules directory and search for a module named underscore. [Note: While referiing to underscore module within node_modules directory we didn't use any relative paths like ./ ]

var underscores = require('underscore')

var sample = require('./sample.js');

console.log(sample.a);

console.log(underscores);

Run node sample2.js and this would print the entire underscore module as below.

Use of package.json

If suppose we are working in a larger project and wanna check in the above files in Version Control System, we probably won't be checking in the node_module dependencies.

Instead ask the Users who check out the code to download the dependencies. This brings in another challenge that the downloadable module dependency list that is associated with the project ought to be maintained in a separate file. This is where package.json comes into the scene.

Move to the Project Root Directory. Here it is Test and run the command npm init. This generates a package.json file carrying all the dependencies listed in local node_modules directory.

Let's try to install another node_module and let it be backbone. Move to the Root Project structure Test and run the command npm install --save backbone

Now let's delete the node_modules directory in local project structure. Move to the root of Project Structure. Here it is test direcotory and run the command npm install. This would dowload all the dependency listed in package.json. That's the beauty of package.json file.

Also if we closely look at the package.json file, we have two types of dependencies

--save saves the dependency to dependencies{} [Dependencies that help in developement]--save-dev saves the dependency to devDependencies{} [Dependencies that helps in deploying, building, testing the application]