An overview of NodeJS

Tram Ho

Hi guys, lately, I have written a series of articles about Javascript. However, in my previous articles, I mainly wrote about ReactJS, or front-end javascript. So today, on a windy day in Hanoi, I would like to write about javascript on the server side. The topic that today I want to introduce to you is NodeJS – an overview.

What is NodeJS?

The following is the definition I found on the homepage of NodeJS:

Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine.

Node.js uses an event-driven, non-blocking I / O model that makes it lightweight and efficient.

Node.js’ package ecosystem, npm, is the largest ecosystem of open source libraries in the world.

As people say, NodeJS is a platform (platform – platform, not a framework) that runs on Google ‘s Google Chrome V8 engine – genuine, don’t ask. When we use Google Chrome browser, Javascript is also compiled using this engine, it will receive your javascript code and translate into machine code, with faster speed. Realizing that this compiler was very fast and efficient, it was decided to put it on server-side, and so in 2009, NodeJS was born. Since then, NodeJS has risen to become one of the top server-side choices, very popular among developers as well as used in major systems such as Netflix. , Paypal, Walmart, and Uber. Following is a chart of the most popular libraries / platforms according to Stackoveflow survey in February 2020.

Top popular libraries / platforms according to Stackoverflow 2/2020 survey

So I have finished explaining the first line of the definition. Now we will continue with the second line

Event driven? Non-blocking IO?

At first, I found out if I heard about asynchronous NodeJS, also known as non-blocking IO, I thought it was something super high. But in fact, they are also very simple. We are too familiar with languages ​​such as C, C ++, Java, .. are languages ​​that run sequentially, that is, everything must happen sequentially, execute the above code, complete and then execute. line of code below. But NodeJS does not, it will use the non-blocking IO mechanism.

I talked through IO a bit. The IO function is the input / output, ie any read / write operations to the file system. IO is a time consuming operation and it will block all other functions running. This is where the non-blocking IO mechanism in NodeJS will come into play.

I will take an example to make it easy to understand: imagine our system receives 2 requests from 2 users, A and B. Both of these two requests are reading data from the database and returning it to the user, no such as viewing personal information. When guy A sends a request to the server, the server reads the database. Usually they create one thread for request A, and another thread for request B, but since NodeJS is single-thread, if not using non-blocking IO then database read operation will block request B, guy B will have to wait until request A is completed before it can be received and processed by the server. But with non-blocking IO, these 2 requests can be made concurrently without having to wait for each other. The non-blocking IO feature also makes it unnecessary to use multithreaded processing and still be able to receive and execute multiple requests at the same time.

Comparison between handling 2 requests of Blocking-IO and non-blocking IO

NodeJS’s asynchronous mechanism offers a huge performance advantage, especially its ability to handle a large number of requests at once. I take for example ASP .NET, a framework that uses blocking IO mechanism, to process many requests at once, they have to generate many different threads, this will make memory and processor resources expensive. more. Of course frameworks like ASP .NET can also handle asynchronously, but we will have to write code to make those asynchronous operations. As for Node JS, asynchronous processing is the default

The NodeJS community is very crowded and aggressive

NPM – Node packages manager is a library system that makes it possible to build apps more quickly and easily. Although Node has only been around for more than 10 years, their support library system – npm has more than 1.4 million packages today, and the number is still growing rapidly (~ 804 packages). / day) according to data of . This figure even outperforms the library systems of established frameworks such as ASP .NET nudget, Ruby on Rails gems, …

Some of the most popular packages when we create a server API with NodeJS are express, mongoose (considered as relation mapping with MongoDB), Sequelize (Relation Mapping with SQL databases), … Only with express with a relation mapping library, we can completely build a RestfulAPI very quickly and easily.

Why NodeJS?

So why is NodeJS growing so strong and popular? I would like to mention some of the following advantages:

  • NodeJS is Javascript, ie with NodeJS, now fullstack programmer only needs to work with one language. Moreover, the stability of Javascript through versions ES5, ES6, then 7, 8 is more and more perfect, stable and brings more features to programmers.
  • The community along with the Javascript ecosystem is extremely strong, even being the largest community according to the stackoverflow survey.
  • Client Server development projects (NodeJS is in the role of services providing APIs), NodeJS is more suitable for microservices architecture to turn big problems into small and playable problems. Quickly develop a large project, develop and manage it with ease. The realtime services it does are also great because realtime systems work instantly with fired events and that is the stage for NodeJS because it has the strength of non-blocking support.
  • The performance of NodeJS is extremely well suited to systems that have to handle multiple requests at once. With the non-blocking model, it saves us a considerable amount of time instead of waiting in queue like the subsidy period.

The above are shares based on my knowledge, hopefully it will help you new to or learn about NodeJS. In the next articles, I will elaborate on issues related to NodeJS.

References

Share the news now

Source : Viblo