Node js: A comprehensive guide to use with examples

Being the most popular programming language, JavaScript is also one of the most universal software development technologies. It has also become a major cross-platform mobile development tool as a basic technology for a large number of platforms, such as React Native, NativeScript, etc. But the areas of application for JavaScript do not end here. Lately, there has been a lot of tool around the use of JavaScript for server-side programming. One of them is Node.js In this article, we will give your a comprehensive guide to use Node js with examples.

Introduction to Node.js

Node.js is an open-source and cross-platform JavaScript runtime environment. It is a popular tool for almost any kind of project. It runs the V8 JavaScript engine, the core of Google Chrome, outside of the browser. This allows it to be very performant.

A Node.js app is run in a single process, without creating a new thread for every request. It provides a set of asynchronous I/O primitives in its standard library that prevent JavaScript code from blocking. Generally, libraries in Node.js are written using non-blocking paradigms, making blocking behavior the exception rather than the norm.

When it performs an I/O operation, like reading from the network, accessing a database or the file system. Instead of blocking the thread and wasting CPU cycles waiting, Node.js will resume the operations when the response comes back. This allows it to handle thousands of concurrent connections with a single server without introducing the burden of managing thread concurrency, which could be a significant source of bugs.

What is Node.js used for?

Node.js is primarily used for non-blocking, event-driven servers, due to its single-threaded nature. For traditional web sites and back-end API services, They designed with real-time, push-based architectures in mind.

An Example Node.js Application

The most common example:
Hello World of Node.js is a web server:!/embed/nodejs-dev-0001-01?path=server.js&previewSize=30&attributionHidden=true&sidebarCollapsed=true

This code first includes the Node.js http module.

It has a fantastic standard library, including first-class support for networking.

The createServer() method of http creates a new HTTP server and returns it.

They set server to listen on the specified port and host name. When the server is ready, the callback function is called, in this case informing us that the server is running.

Whenever a new request is received, the request event is called, providing two objects: a request (an http.IncomingMessage object) and a response (an http.ServerResponse object).

Those 2 objects are essential to handle the HTTP call.

  • The first provides the request details. In this simple example, this is not used, but you could access the request headers and request data.
  • The second is used to return data to the caller.

In this case with:

res.statusCode = 200

we set the statusCode property to 200, to indicate a successful response.

We set the Content-Type header:

res.setHeader('Content-Type', 'text/plain')

and we close the response, adding the content as an argument to end():

res.end('Hello World\n')

The Benefits of Node.js

Rich ecosystem

One word – npm, a default Node.js package manager, it also serves as a marketplace for open source JavaScript tools, which plays an important role in the advance of this technology. With about 836,000 libraries available in the npm registry as of now, and over 10,000 new ones every week, the Node.js ecosystem is quite rich. The same stats point out that 97 percent of modern web applications consist of npm modules. And that’s proof of its undisputable popularity among developers.

With such a vast variety of free tools accessible in a few clicks, there is a huge potential for the use of it. At the same time, open source software enjoys growing popularity as it allows you to build new solutions reducing the overall costs of development and time to market.

Robust technology stack

JavaScript has proven to be an undisputed leader among the most popular programming languages. In turn, Node.js has become a stand-alone name in the industry. With a total of 368,985,988 downloads and over 750 new contributors as stated in Node-by-numbers report 2018, the project seems to be stronger than ever.

Using it for backend, you automatically get all the pros of full stack JavaScript development, such as:

  • better efficiency and overall developer productivity
  • code sharing and reuse
  • speed and performance
  • easy knowledge sharing within a team
  • a huge number of free tools

Consequently, your team is a lot more flexible. So that the development is no longer time-consuming. As a result, you get fast and reliable software. Moreover, developers trained in frontend JavaScript can start programming the server side with minimum effort. With the same language on both sides, you can reuse code on the frontend and the backend by wrapping it into modules and creating new levels of abstraction.

Scalable technology for microservices

Since it’s a lightweight technology tool, using Node.js for microservices architecture is a great choice. Martin Fowler and James Lewis described it as “an approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API.

Accordingly, breaking the application logic into smaller modules, microservices, instead of creating a single, large monolithic core, you enable better flexibility and lay the groundwork for further growth. As a result, it is much easier to add more microservices on top of the existing ones than to integrate additional features with the basic app functionality.

The difference between the monolithic and microservices architecture

Monolithic architecture vs microservice architecture in a nutshell

It is the technology of choice when building and deploying microservices solutions, according to Node.js User Survey Report 2017. About half of the respondents are using microservice-related technologies (namely, Docker, the leading software containerization platform) to build Node.js web apps:

Number of developers using docker with node.js

Percentage of developers using Docker containerization with Node.js

More recent findings show that microservice-related technologies such as Docker and Kubernetes experienced growth in usage in 2018, as this architectural style gets only more popular. With each microservice communicating with the database directly through streams, such architecture allows for better performance and speed of application. A match made in heaven is supported by two frameworks widely used for microservice architecture. The Express framework lists IBM and Uber among its users.

As an example of live implementation, Walmart’s shift to microservices architecture with Node.js resulted in the following immediate benefits:

  • Overnight 20 percent conversion growth in general and 98 percent mobile conversion growth
  • One hundred percent uptime on Black Friday (handling over 500 million page views)
  • Saving up to 40 percent on hardware and 20-50 percent on overall operations

Fast-processing and event-based model

Node.js is fast; it is not a myth. There a couple of reasons for it showing such results:

  • V8 engine. The engine used in Node.js implementation was originally developed for the Chrome browser. Written in C++, Chrome’s V8, they use it to compile functions written in JavaScript into machine code, and it does the job at an impressive speed.
  • Non-blocking Input/Output and asynchronous request handling made it capable of processing requests without any delays. In the context of backend, synchronous processing assumes that code is executed in a sequence. Thus, each request blocks a thread, making other requests wait for it to finish. Asynchronous processing allows processing request without blocking (non-blocking I/O) the thread. So after processing a request, it can push out a callback and continue serving requests. That helps it make the most of single threading, resulting in short response time and concurrent processing.

Strong corporate support

To clarify, Joyent supported the development of Node.js. In 2015, the Node.js Foundation was created to “enable widespread adoption and help accelerate the development of Node.js.” IBM, Microsoft, PayPal, Fidelity, and SAP became the founding members of the organization.

The list of organizations using it in production is constantly growing. It currently includes almost three hundred well-known companies, such as PayPal, Medium, Trello, Uber, and Zendesk.

Very few open source projects have ever enjoyed such strong support from the world’s leading companies. And that foretells it has outstanding potential.

Seamless JSON support

Although other backend technologies like PHP and Ruby on Rails can use JSON format for communication, Node.js does it without converting between binary models and uses JavaScript. This is especially handy when you need to build RESTful APIs for NoSQL database support, like MongoDB – the letter M in the MEAN stack. This seamless communication with one of the main data transfer standards is another advantage of the JavaScript ecosystem.

Where Node.js Shouldn’t Be Used


Comparing Node.js with Express.js against Ruby on Rails. For example, there used to be a clean decision in favor of the latter when it came to accessing relational databases like PostgreSQL, MySQL, and Microsoft SQL Server.

Relational DB tools for it were still in their early stages. On the other hand, Rails automatically provides data access setup right out of the box together with DB schema migrations support tools and other Gems (pun intended). Rails and its peer frameworks have mature and proven Active Record or Data Mapper data access layer implementations.


To sum up, we believe this blog has already provided you the necessary information to your project. however, if you still have any questions about this article or your project development, you can fill this CONTACT FORM. ArrowHiTech will give you the answer with the shortest time as possible. The most important thing in this blog is please remember to explore our Web Application Development Services including Node JS Development.