Node.js Tutorial for Beginners

Abhigyan Singh 01st Oct 2020

Node.js has been popular these days due to its a 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 the right place will clear all doubts about it. In this tutorial we are going to describe the 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 programs?
  • How we install modules of Node.js and use them.

What is Node.js?

Node.js is a 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 the V8 engine 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, keypress, or a message from the operating system or another program.
The non-Blocking Model refers to operations that don’t block further execution until that operation finishes. We will describe it below after completing the installation.

Who is using Node.js?

Many organization is using Node.js like LinkedIn, PayPal, Uber, and Netflix. Mozilla has implemented 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 a node.HP uses a 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.

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

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

Import modules:

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

Here Http is a module that is used to create Http instance into the Http variable.

Create Server:

After creating an instance, we will call Http.createServer() method to create server instance after that bind it to port 8081 using listen to 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 the 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 https://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 an example of a 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 the result, run main.js into the terminal will show the 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 queries regarding node.js, feel free to write us in the comment section.

Authored By Abhigyan Singh

He is a continuous blogger and has blogged on different topic. He loves to surf Internet and always trying to get new Idea about new Technology and Innovations and sharing these great information to all the technology lovers.

Also on DiscussDesk