What is Node.js?

What is Node.js
What is Node.js

Node.js is an open source and cross-platform run-time environment for JavaScript. JavaScript is a language, specially designed to work at the front-end side of the web. Because previously it was used only for the client-side scripting to make websites more interactive, different browsers are using different parsers to support JavaScript like chrome browser is using the V8 engine, Mozilla is using SpiderMonkey, and Internet-explorer is using Chakra.

But in 2009 developer called Ryan Dahl came up with an amazing idea and developed Node.js to extend support for the JavaScript from only used at client-side scripting to work as a standalone application and on server side development. The main idea behind the creation of Node.js is to run JavaScript code out of the browser. Node.js provides a run-time environment for the JavaScript, so now can run and execute your JavaScript code on any machine using Node.js. Now using Node.js you can do much more things using your JavaScript skill than just using it only at client-side scripting to make websites interactive.

Ryan Dahl had started working on Node.js in 2009 by using Google’s V8 engine which is an open-source JavaScript engine used in Google Chrome browser. Rayn Dahl took V8 engine and embedded it into the c++ program and called it Node.js. After working on a Node.js project for three years finally Node.js was announced in 2012.

Because JavaScript was originally designed for client-side scripting it has some objects and methods which are only useful for front-end side scripting like document.getElementById() but not useful outside the browser. But in Node.js Those objects are removed and new object and features like a filesystem, creating server are added to support serverside development etc.

What is the use of Node.js

Because Node.js is developed to run JavaScript code outside of the browser on any machine you can now do much more things with JavaScript. Using Node.js you can do things that you can with scripting languages like Python.

Here are some operations you can perform using Node.js:

  • You can listen to a request from client-side
  • Node.js can respond according to a user request
  • Using Node.js you can generate dynamic page content
  • You can collect Form data from users
  • Using file system, Node.js can create, delete, open, close, read, and write files on the server
  • Using Node.js you can also work with different database systems. You can create a new database and add, delete and modify the data in the database.

Previously languages like PHP, Asp.net, and java was being used for server-side development but now using Node.js server-side development can be done using JavaScript.

Node.js now can also help you to create applications for multiple platforms like Android and Ios using frameworks like React-native. React-native is a framework developed by Facebook to create apps for different platforms using JavaScript.

Creating a chatting application using Node.js is also very easier and efficient. You can add chatroom in an application using Node.js.

You can develop different browser-based and JavaScript-based games using Node.js

Collecting data from the users and storing them in the database is also one major task you can perform using Node.js

Video and audio streaming is also done using Node.js

Other than that you can create and built so many things using Node.js using your development skills. It is up to you that what can you built using it.

What is NPM

NPM(Node Package Manager) is a package manager for Node.js packages and modules. You can create a custom package for later use or you can also use from already available packages. There are over 600 thousand packages are available for use. www.npmjs.com hosts those free packages to download and use. You can also add your custom packages for public use or private use.

NPM packages contain pre-written code-blocks that you can you in your program. Packages contain different modules that you can use in your javascript code. Modules are JavaScript libraries you can include in your projects.

The main goal of NPM is to provide support to developers and speed up the Node.js development. There are thousands of packages are available which makes your development easier and fast, and gives you more features and functionalities to built your projects using Node.js.

To use different NPM packages you have to download it first for your project. You can install different NPM packages using a simple command on your command prompt. Use npm install packagename -g  (here package name is a name of NPM package you want to install) command to install NPM package globally on your machine because here -g is used to install it globally. But instead of installing it globally you can install it in the specific directory by removing -g from your command and to install in the specific directory you must have to be in that directory in command prompt. And also make sure your internet connection is working while installing NPM packages.

There are so many popular and widely used NPM packages are available like:

Express.js: Express.js is a server framework for web applications. You can develop single-page, multi-page and hybrid web applications. It is a widely used Node.js framework.

Body-parser: Body-parser is node.js body parsing middleware. This NPM provides JSON body parser, Raw and text body parser, URL-encoded form Body parser.

Nodemailer: Nodemailer package is used for mailing service. You can create and send mail through nodemailer. You can choose a mailing service you want for sending mail using nodemailer.

FS: FS package provides modules to handle file systems. Using FS you can open, close, read, write and delete files on a system.

Lodash: This package makes your work hassle free if you are working with JavaScript arrays, numbers, objects, strings, etc.

How Node.js works?

Now, it is a very interesting topic ,how Node.js actually works. So let’s understand it properly.

Node.js files are creates with .js extension. When you execute your .js program using Node.js the Node.js takes your code and converts it into the machine code. Because machines can understand only machine code.

Node.js supports single-threaded programming. Whereas other languages like PHP and Java provides multi-threaded programming. Now let’s understand what is single threaded and multi-threaded.

In the single thread, it has only one thread to perform and work for multiple requests. Only one single thread provides a response for multiple requests one by one because it can’t create multiple threads. But the problem with a single thread is that it can’t take the second request until the first request is processed and it is not good when you are dealing with multiple requests.

Whereas in multi-threading it creates multiple and individual threads to serve different requests. Because of its multi-threaded nature, it can handle multiple requests simultaneously.

But what now, because Node.js is single threaded, Is it slow? and I shouldn’t use it for my project? then answer is the big NO. Why NO then? let’s understand.

Node.js uses single threaded Non-Blocking and event looping I/o Model. And this is what makes it faster, efficient and light-weight. Now let’s understand what is Blocking and Non-Blocking model.

First of all, let’s clear it that I/O stands for Input and Output. It can be HTTP request and response, local file reading and writing or any other Input-Out process.

What is Blocking( synchronous) I/o model?

Let’s understand blocking I/O model by an example. In any hotel, a waiter takes order from one customer. If another customer comes to the hotel then a different waiter is allocated for service. Without completing an order of selected customer waiter can’t handle other customers. he takes the order, gives it to the chef and waits for the dish preparation and when the dish is ready then he serves to the customer. This is called blocking model means here one waiter is blocked for one customer.

To handle multiple requests multiple threads are created and allocated to an individual process. Without completing given task it can’t handle other requests. But what if multi-threading is not supported?. Then it performs very slow.

What is Non-Blocking(asynchronous) I/O model?

Now let’s understand the Non-blocking I/O model with same hotel example. There is only one waiter available in hotel and more than one customers arrive at the hotel. Now in Non-blocking model waiter takes orders one by one but without blocking himself. He takes order from one customer and gives to a chef, then he takes an order from the second customer and gives to a chef and keeps doing for every customer without blocking himself for only one customer. When the dish is ready then he provides to the customer. So here waiting time for preparing dish is eliminated.

The same way Non-blocking model works for multiple requests. It takes requests one by one without blocking and provides to the server until server completes given process. When a process is completed then it provides a response. Here even single thread performs better.

Here Node.js follows asynchronous means Non-blocking model so it handles multiple requests with single thread without blocking. So this makes Node.js faster and efficient.

Features of Node.js

  • Open-source
  • Cross-platforms support
  • Single threaded
  • Follows Non-blocking I/O model
  • Extension of Node files are .js
  • Fast and efficient

A simple program in Node.js

Here we are creating a Server.js file in which we will create the local server:

var http= require(‘http’);
var server= http.createServer( function(req, res) {
res.end(‘Hello Node.js’);
});
server.listen(3030);

About Geek Kishan

Tech enthusiast | Geek | Programmer | Web developer | Introvert. Love to read and write about technology. I want to share my little knowledge with the world hoping that it will help someone.

View all posts by Geek Kishan →

Leave a Reply

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