Node.js | Benefits | Why | Where to use | setup | example | module | Getting started

Node.js is one of the most popular framework today. Whenever we think about development of a successful and responsive web application, the term node.js is very common. Let us have a detailed look on Node.js to easily understand what node.js actually is and how it work. After completing this article we will be familiar with this framework and its application. Let us start with an introduction.

What is node.js?

The typical answer we get is “Node.js is a JavaScript runtime environment.” What does that mean? Node.js is a platform used to develop I/O intensive web applications like single-page applications, video streaming sites and other web applications. It is built on Google chrome’s V8 engine. It is very powerful framework based on JavaScript and used by thousands of developers around the world.

Node.js is a server-side open-source cross-platform runtime environment developed by Ryan Dahl in 2009. It was developed for creating fast and scalable network application easily. It is based on event driven, non-blocking I/O model that is the reason why it is light-weighted and efficient. It is suitable for real-time data-intensive applications that run across distributed devices.

Benefits of using node.js

  • Open-source server environment i.e. free of cost
  • Support cross-platform i.e. Windows, Linux, Mac OS X etc.
  • Rich library of various JavaScript modules i.e. simplifies web application development
  • Built on Google chrome’s V8 engine i.e. very fast code execution
  • Output data in chunks without buffering
  • Released under MIT License
  • Highly scalable
  • Work on single thread
  • Use non-blocking

Why node.js?

  • As we know node.js contain a rich library, all the APIs in the library are asynchronous i.e. non-blocking. It never wait for an API to return data. It is based on server instead. The server call an API and move to the next API calling without waiting for the response from that API. The notification mechanism of events help the server to get response from previous API call. This event driven asynchronous programming feature make it a better choice.
  • It allows the server to work asynchronously by event mechanism that makes it highly Scalable. It uses a single threaded program and the same program can provide service to a much larger number of requests than traditional servers.

Where to use node.js?

  • Data Streaming Applications
  • Single Page Applications
  • Data Intensive Real-time Applications
  • I/O bound Applications
  • JSON APIs based Applications

node-js-logo-MSA-Technosoft

Node.js setup

  • Go to the official website of node.js https://nodejs.org/en/
  • Download and install setup file for your system

Node.js module

Before creating our first example we must have an idea about the components of node.js. It consists of 3 main components:

  1. To import require modules using “require()” method
  2. Create server
  3. Read request from client and return response by server

Getting started with node.js

Step1: Include the module first.To import required http module, require() method is used along with the name of the module. The following snippet is used for this purpose:

var http= require(‘http’);

Step2: Server initialization is must for receiving client request and give response back to the client via server. To create server we use createServer() method with the instance of http created in first step. To initiate server in node.js following code snippet is used:

http.createServer(function(myRequest,myResponse)

{

myResponse.writeHead(‘200’,{‘Content-Type’: ‘text/html’});

myResponse.end(‘My First Example’);

}).listen(8080);

*200 shows the Server connection is OK.

*8080 is the port number where server will execute in your system

Step3: Let us now start with our first example here. Open a text editor to write all above code and save the file with extension “.js” as a JavaScript file. Let us save with file name FirstExample.js

To show the output in web browser console.log() method is used.

var http= require(‘http’);

http.createServer(function(myRequest,myResponse)

{

myResponse.writeHead(‘200’,{‘Content-Type’: ‘text/html’});

myResponse.end(‘My First Example’);

}).listen(8080);

Console.log(‘This is my First Example’);

Step4: To start the server, run FirstExample.js in Command Line Interface of your system. For example Command prompt(cmd) for Windows OS. Open the terminal and change the directory to where your file is located. Now, Use the code snippet below in your terminal:

node FirstExample.js

Console will print the message “This is my First Example” to confirm that the server has started.

Now, your computer works as a server. If anybody try to access your computer on port 8080, they will get the message ‘My First Example’ as response.

Open your web browser now and type the URL http://127.0.0.1/8080/

 

Was this article helpful? We would love to hear from you. share your views in the comment section below.

visit Tech Blog to know about all our technology blogs.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.