Node.js Tutorial for Beginners

Node.js has been popular in these days due to it’s certain feature. Here we will describe all features of Node.js. If you are willing to learn to Node.js framework then you are at right place will clear all doubt about it. In this tutorial we are going to describe following points of Node.js which is easily understandable to beginners:

  • What is Node.js?
  • Why we use it?
  • Who is using Node.js?
  • Where to use Node.js?
  • How we install node.js and run Program?
  • How we install modules of Node.js and use them.

What is Node.js?

Node.js is server side JavaScript environment built on Chrome’s V8 JavaScript engine. V8 is Google’s open source high performance JavaScript engine. After adding some functionalities using V8 engine, Node.js was built to remove some drawback of traditional languages.

Why we use it?

Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Event- driven programming is programming paradigm in which flow of program execution is determined by events – for example a user action such as a mouse click, key press , or a message from the operating system or another program.
Non-Blocking Model refers to operations that doesn’t block further execution until that operation finishes. We will describe it below after completing installation.

Who is using Node.js?

Many organization are using Node.js like LinkedIn, PayPal, Uberand Netflix. Mozilla has implemented Node.js to support browser APIs which has half a billion installs.Pinterest, one of the hottest new startups uses node as a webserver in some instances.EBay hosts their HTTP API service with node.HP uses node for their WebOS initiatives.

When to use Node.js?

1.    If you are doing non-blocking operation and does not have heavy algorithm/Job which consumes lot of CPU cycles.
2.    If you are comfortable in writing single threaded code just like client Js.
3.    Applications which is based on JSON APIs.

How we install node.js and run Program?

To install node.js, go to https://nodejs.org/en/download/current/ . You will have following page as shown below. Download from there according to operating system what you have.

 

 

To Print Hello node in Node.js, follows these steps:

    1. Create a main.js file with following is code and save it where you want. In my case I save in node folder which is in htdocs.

 

      2. Now in cmd, give the file path to reach the file. After reaching file location enter file namewith node word as shown below. As you are seeing,You have printed Hello Node in main.js.

To see output of main.js file on browser you have to do following things explaining below.

A Node.js Application consist of following three important parts:

  • Import modules
  • Create Server
  • Read Request and Return Response

Import modules:

Encapsulate related code into single unit of code called module. In node.js you can load module according to your requirement. We use require directive to load module as shown below:
varhtttp = require("http");

Here htttp is module is used to create http instance into http variable.

Create Server:

After creating instance, we will call http.createServer() method to create server instance after that bind it to port 8081 using listen method as shown below:

htttp.createServer(function (request, response){
//send HTTP header
//HTTP Status: 200: OK
//Content Type : text/plain
response.writeHead(200,{'Content-Type' : 'text/plain'})
//send respond 
response.end('Hello Node\n');
}).listen(8081);
//Console will print the message
console.log('Server running at http://127.0.0.1:8081/');

Read Request and Return Response:

Create test.js then Merge import module and create server code to one file to create HTTP Server as shown below:

varhtttp = require("http");	
htttp.createServer(function (request, response){
//send HTTP header
//HTTP Status: 200: OK
//Content Type : text/plain
response.writeHead(200,{'Content-Type' : 'text/plain'})
//send respond 
response.end('Hello Node\n');
}).listen(8081);
//Console will print the message
console.log('Server running at http://127.0.0.1:8081/');

 

Now Execute the test.js file from cmd or terminal to start the server at port 8081 as follows:

node test.js

The output will be on terminal as shown below:

To run this program on browser enter http://127.0.0.1:8081/ in address bar as shown below will show same output as shown below:

Congratulations, you have your first HTTP server up and running which is responding all the HTTP requests at port 8081.

Let’s take another example to understand non-blocking system in node.js. Create a file content.txt with following content:

This is example of non-blocking system of Node.js.

Create a js file main.js which has following code:

varfs = require("fs"); // loading module
var content = fs.readFileSync('content.txt'); 
console.log (content.toString());  
console.log('Program Ended');

Now run main.js file to see output with following command:

node main.js

In this case content of content.txt file will read then it will print program ended as shown below:

Above code will not print Program Ended until entire content of content.txt file is read. So it block further code until previous line of code is read while in Node.js doesn’t block execution of code can understand by putting following code in main.js file.

varfs = require("fs");
fs.readFile('content.txt', function (err, data) {
if (err) return console.error(err);
console.log(data.toString());
});
console.log("Program Ended");

To see result, run main.js into terminal will show following output:

Above example shows non-blocking system of node.js by printing program ended firstly after that it reads content of content.txt file.

If you have any query regarding node.js,feel free to write us in comment section.

Share this Article on Social Media

All of my Scripts are ready to customized as per your requirement. Feel free to contact for script customization.

Contact me at discussdesk@gmail.com

"Note : It will be charged as per your customization requirement :)"

Get Updates, Scripts & Other Tutorials to Directly to your Email

Over 20000+ Happy Readers already subscribed. (We don't send spam email). Every email subscriber can get our latest updates and download our 100+ scripts.

Comments