Node.js is a system that executes JavaScript separately from your browser. We can say that it is a stand-alone JavaScript execution environment.

Node.js can be installed on a server (just like Python) and your code can be executed on it, giving the result of execution to users. On it, you can create separate applications using additional frameworks.

Why Node.js is good


If you started learning programming, you will understand the concept of sequential program execution. First, the first line is executed, then the second, then the third, and so on. Maybe somewhere the program will jump back and forth, somewhere it will loop, but in general it is executed linearly.

JS has a trick – it can execute code in parallel (relatively speaking). Programmers call this event handling. You can tell the program: “If this ever happens, execute that function, it knows what to do.” Many such events can be foreseen – it turns out that the processes seem to be parallel. The program can just sit and wait for something to happen.

Moreover, the processes are asynchronous: the program can request something at the very beginning of work – for example, ask the database for something. And while the database itches, the Node.js program will go on with its own business. And when the answer comes, it will take it and process it.

When developing any Node.js program, there can be hundreds of such concurrent events and handlers. To prevent any of them from pulling the blanket over themselves, Node.js makes an endless loop, in which it provides CPU time to each function in a circle. As a result, the illusion is created that they work in parallel and do not interfere with each other, but in fact they are rigidly controlled by the platform itself.

It is this even distribution of everything in the event loop that gives Node.js an edge when building server-side applications.

How a Node.js server works

  1. On the page, for example, you need to show the user’s avatar and nickname.
  2. To do this, the server makes a request to the database to get this information from there, and it itself continues to form the page further.
  3. Until the avatar arrived, the server had already done the rest and saw that a background image was needed.
  4. The server requests a picture, and at this time it received an avatar from the previous request. Once the avatar has arrived, the server inserts it in the right place. While he was doing this, the background picture came.
  5. All materials are in place, you can show the page to the user. The page loads faster because the build happened in parallel.

What else is Node.js for


In addition to web applications, this language is used to create any services where a constant exchange of information with the user is needed:

  • chats,
  • social networks,
  • project collaboration systems,
  • online text editors.

And you can write almost any application on it and run it under Windows, MacOs or Linux. This can be used to develop universal applications, such as a to-do list that should work across all platforms, sync data in real time, and be able to send everything to a mobile device.

Recently, the use of Node.js for the “Internet of things” – smart kettles, scales and other household appliances – is gaining popularity. This is where Node.js helps manage these appliances and make servers that can handle many requests at the same time.

Leave a Reply

JavaScript

06/01/2021