Beginners Introduction

Why Use Node.js? | Beginners Introduction to Node.js

Why Use Node.js? | Beginners Introduction to Node.js

Node.js is a runtime environment for JavaScript that is free, open-source and cross-platform used for server-side scripting to build complex network applications.

Now, before getting into learning the nitty-gritty of Node.js, let us first understand why it is advisable to use Node.js. 

Why use Node.js?

There are several reasons why you would want to use Node.js. Some of the benefits of using Node.js are as follows:

  1. It has strong technology support
  2. Better and faster processing because of event-based modeling
  3. Scalability
  4. Node Package Manager (NPM)
  5. Widely accepted

1. It has strong technology support

Node.js isn’t any framework or programming language per se. It is a runtime environment for JavaScript scripts to run on the server-side. JavaScript is among the most popular and robust programming languages being widely used. Node.js has been able to prove itself as a stand-alone technology in the web development industry because by using Node.js you can enjoy all the features of a full-stack JavaScript development. It improves the productivity of the developers as it allows code reusing and sharing. Thus, it increases the speed of the development process. JavaScript is an easy language to learn and so, sharing knowledge with the team can be done easily for training purposes. Developers that were before trained just for using JavaScript for front-end can easily start working on the back-end; perhaps reusing the same code as used for front-end. Thus, the development can be done in a lesser amount of time producing better and faster applications. 

2. Better and faster processing because of event-based modeling

The most important reason for faster processing of Node.js applications is that they run on a V8 engine. This engine was originally developed for Chrome browser; written in C++, the V8 engine compiles the functions written in JavaScript and it does this job at an excellent speed. This is because Google has already worked pretty hard in creating this engine and also, it keeps on bringing updates to the engine annually. Node.js just gets all the benefits out of it directly. Apart from this, Node.js handles the requests in a non-blocking manner i.e. asynchronously. In synchronous processing, each request has to wait while the one before is finished processing as the thread is locked. This sequential execution requires a lot of time. While in asynchronous processing, the requests are processed without blocking the thread and so, once a task is done, it can immediately start serving the next request and thus, it uses the single thread in the maximum way it can. Thus, the response time decreases by a great amount and it makes it possible to have concurrent request processing. 

Node.js has event-based modeling. Anything and everything is done only if an event occurs. To every event, a callback function is associated which has the task to be done upon the occurrence of such an event. The asynchronous behavior helps in processing these callback functions fast.

You can also use an Event Emitter to explicitly fire an event to which anyone can listen. An event can be emitted multiple times and you can configure your application to listen to it only the first time and not call the callback function the other times the event is triggered.  

3. Scalability

Node.js is a very light-weight technology and so, it can be used to have micro-service architecture. IT is more suitable to have a single application that houses multiple microservices, each for a small task to have code reusability and flexibility. Node.js is a perfect choice for this kind of architecture. This makes it very easy to integrate the updates. The new micro-services can be added to the already deployed application very easily without requiring many changes to be done to the original code. So, it is not compulsory to have a monolithic architecture. Having micro-services also improves the response time of the application. Also, a large number of requests can be served concurrently without having a hardware downtime. There are several use-cases that prove this.  

4. Node Package Manager (NPM)

Node.js has got a very rich ecosystem. NPM is like an open-source, free marketplace that has all the JavaScript tools. There are more than a million libraries available in the NPM registry which can be downloaded and used in a single click. These libraries decrease the overall cost and development time of the applications built using Node.js.

5. Widely accepted

Node.js is very popular in the industries. Many organizations are using, and also promoting the use of Node.js for web development. This increases the chances of your open-source project getting help from the leading companies across the globe.

These are the most commonly know benefits of Node.js. But, the actual list is very long. Now that you have a basic idea about the benefits of Node.js, you might be interested in using it in practice.  

The following passage is a guide for beginners of Node.js. It will walk you through the installation process, sample application, how to use NPM, and much more. 

How to install Node.js?

For Windows users, go to Node.js for Windows to download the package, while Linux users can directly use the installer. You should check the GitHub repository to see if there are any updates available from time to time. 

Once you are done with downloading and installing Node.js, you can test the installation by running the command ‘node -v’ on your command prompt. In case of any kind of error you might want to refer to this link. 

In the similar way you can also download and install NPM – Node Package Manager. NPM can be used for downloading any further updates in Node.js as well as other packages that you would want to use in your applications. 

Now that you are done with installing Node.js, let us see how to build an application. For demonstration let us create a ‘Hello World!’ application. 

Steps to create a Node.js application – Hello World Application 

  1. Open the terminal if on Linux, and the Command Prompt if on Windows. Change your current directory to the location where you want to create your application. Create a folder named ‘hello_world’
  2. Change your current directory to the project’s directory – the one we just created. In this folder, create a file named app.js.
  3. Now, paste the following content in the app.js file that we just created.
  4. By doing so you created an HTTP Server that you will need to run the application. The argument to the create server function is the callback function that is to be called upon creation of Server. In this function, we are printing plain text – ‘Hello World!’ This is added to the response variable that will be returned. The listen method specifies the port number on which the server has to listen to the requests of this application. For any HTTP server, the default port number is 80, but it is advisable to explicitly specify a different port number. 
  5. Now that we have written the code for our application, we are only left to run the application and check the output. IN order to run the application, use the following command:
  6. Now, to see the output, go to the port you have specified, here 4000.

Go to the browser and type ‘http://localhost:4000/’in the URL space and hit Enter to see the output. You will see ‘Hello World!’ in the browser window.

Hurray! You now have a server up and running. Host create your application and do wonders with Node.js

What is NPM?

NPM stands for Node Package Manager. As its name suggests, it manages the packages of Node. It has a great number of packages/libraries that can be used in Node.js. These packages can be installed with NPM and used. 

Using the –save option saves the package in your local system so that it can be used in the future without having to download it again. 

The first and foremost command that you should run is ‘npm install’. It will look for all the packages listed in your package.json file and install them automatically, saving you the trouble.

How to use packages?

By using the ‘require’ function, you can include the package in your code and store it in a variable that will let you use it. 

const http_package = require(‘https’)

Using require, we can load modules that come by default with Node.js, the third-party packages that we have installed for our use, as well as our local files. Thus, it creates a room for fine-grained applications. In fine-grained applications, we can create micro-services and integrate them using ‘require’. 

Node.js can be used with many frameworks that make it easier to create and use a web server. Express Framework is one of the most widely used frameworks for Node.js applications. Express is widely used as one of the components of MEAN (MongoDB, Express, AngularJS, Node.js) stack web applications.

Node.js can be used for many real-time applications such as Chat Application, Proxy, Data Streaming, Brokerage – Stock Traders’ Dashboard, Other Monitoring Dashboards, etc. In short, Node.js can be used for most of the server-side web applications. But, if the server uses a Relational Database than Node.js is not a wise choice as it hinders the Non-Blocking I/O of Node.js and thereby refrains concurrent request processing. Also, if the computations on the server-side are very heavy, then Node.js is not a wise choice as Node applications are driven by a single thread and single core of CPU; this decreases the speed of computation and increases processing time. After all, Node.js was never created to solve computation problems. It was created to solve I/O problems. So, use Node.js if your application has more importance of the response time than computation and get the benefit of Node’s scalability to get fast non-blocking I/O. 

Leave a Reply