Thursday, July 28, 2016

Node.js

What is Node JS?
Node.js is powerful JS Framework built in chrome’s v8 engine. It is used to develop I/O intensive web applications like video streaming sites, single-page applications, and other web applications. Node.js is open source, completely free, and used by thousands of developers around the world. This article will give you enough learning of node.js. Before moving further one should have basic knowledge of JavaScript.
Node.js was developed by Ryan Dahl in 2009 and its latest version is v0.10.36.
Node.js is a server side platform built on Google Chrome's JavaScript Engine (V8 Engine). Node.js is a platform built on Chrome's JavaScript runtime for easily building fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that is able to load in every devices.
Why to use it?
Following are the areas where Node.js is proving itself as a perfect technology partner.
  • I/O bound Applications
  • Data Streaming Applications
  • Data Intensive Real time Applications (DIRT)
  • JSON APIs based Applications
  • Single Page Applications

How to use Node JS?
Download latest version of Node.js installable archive file from Node.js Downloads. At the time of writing this tutorial, following are the versions available on different OS.
OS
Archive Name
Windows
node-v0.12.0-x64.msi
Linux
node-v0.12.0-linux-x86.tar.gz
Mac
node-v0.12.0-darwin-x86.tar.gz
SunOS
node-v0.12.0-sunos-x86.tar.gz

Verifying installation.
Create a js file named 1st.js on your machine (Windows or Linux) having the following code.
/* Hello, World! program in node.js */
console.log("Hello, World!")

Now execute main.js file using Node.js interpreter to see the result:
$ node 1st.js

It should produce the following result:"Hello, World!"
Import required module.Example
We use require directive to load http module and store returned HTTP instance into http variable as follows –
var http = require("http");

Create server.
At next step we use created http instance and call http.createServer()method to create server instance and then we bind it at port 8081 using listenmethod associated with server instance. Pass it a function with parameters request and response. Write the sample implementation to always return "Hello World".

http.createServer(function (request, response) {

   // Send the HTTP header
   // HTTP Status: 200 : OK
   // Content Type: text/plain
   response.writeHead(200, {'Content-Type': 'text/plain'});
  
   // Send the response body as "Hello World"
   response.end('Hello World\n');
}).listen(8081);

// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');


Request and Response Test
Let's put importing and creating server together in a file called 1st.js and start our HTTP server as shown below –
var http = require("http");

http.createServer(function (request, response) {

   // Send the HTTP header
   // HTTP Status: 200 : OK
   // Content Type: text/plain
   response.writeHead(200, {'Content-Type': 'text/plain'});
  
   // Send the response body as "Hello World"
   response.end('Hello World\n');
}).listen(8081);

// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');


Now execute the main.js to start the server as follows -
$ node 1st.js

Verify the Output. Server has started
Server running at http://127.0.0.1:8081/

Open http://127.0.0.1:8081/ in your browser to see the result.
REPL terminal

REPL stands for Read Eval Print Loop and it represents a computer environment like a window console or Unix/Linux. It performs the following desired tasks.

Read - Reads user's input, parse the input into JavaScript data-structure and stores in memory.
  • Eval - Takes and evaluates the data structure
  • Print - Prints the result
  • Loop - Loops the above command until user press ctrl-c twice.

REPL feature of Node is very useful in experimenting with Node.js codes and to debug JavaScript codes.

Other interesting things to know?
Features
Following are some of the important features that make Node.js the first choice of software architects.
  • Asynchronous and Event Driven - All APIs of Node.js library are asynchronous that is, non-blocking. It essentially means a Node.js based server never waits for an API to return data. The server moves to the next API after calling it and a notification mechanism of Events of Node.js helps the server to get a response from the previous API call.
  • Very Fast - Being built on Google Chrome's V8 JavaScript Engine, Node.js library is very fast in code execution.
  • Single Threaded but Highly Scalable - Node.js uses a single threaded model with event looping. Event mechanism helps the server to respond in a non-blocking way and makes the server highly scalable as opposed to traditional servers which create limited threads to handle requests. Node.js uses a single threaded program and the same program can provide service to a much larger number of requests than traditional servers like Apache HTTP Server.
  • No Buffering - Node.js applications never buffer any data. These applications simply output the data in chunks.



Post a Comment