Node.js is a rising star in programming. In this article, we will explain what Node.js is and what features it has.

Node.js is not a separate programming language, but a JavaScript execution environment. Or, in other words, a server-side JavaScript framework. In addition to the name “Node.js”, you can also find a slang name – Node (node). The platform was created by Ryan Dahl, and the development was sponsored by Joyent. Wednesday was introduced in 2009.

Previously, only the browser could execute JavaScript. With node.js, you can do this without it. At the moment, PayPal, eBay, Amazon and Netflix use this technology for their backend.

Most often, Node.js is used to write the server side of the site (for which it was originally created), but now desktop programs are also written on it.

Node.js is based on the V8 engine. V8 is a program that converts JavaScript into machine code, that is, code that a processor can understand. The same engine is used at the core of the Google Chrome browser. V8 alone is not enough for convenient work, since it can only work with JavaScript. He cannot read files, cannot work with the network outside the browser, so Ryan Dahl, together with the developers, using additional libraries and code, added additional features to V8. This allowed us to make a web server from JavaScript.

Pros of Node.js:

  • Using the same JavaScript language both on the client side in the browser and on the server.
  • NPM (Node Package Manager) is a package manager, or a set of ready-made modules. It is a kind of warehouse where programmers share their code. The community is very large, so at the moment NPM has collected a huge number of solutions for different tasks.
  • Increase the speed of work. Node.js is a single threaded and asynchronous system. You can read files, send emails, request data, and do other things at the same time. We will tell you more about single-threaded work below.

Node.js fundamentals: what is multi-threaded and single-threaded work

When studying server processes, you need to know that there are 2 schemes for working with a server – multi-threaded and single-threaded.

Working with a web application is sending a request and receiving a response from the server. A multi-threaded system is linear. First, one request is executed, then another, and so on in turn. For example, we need to refer to a database. The web server makes a request and waits for a response. Once the response is back, it processes it and can start sending other requests. As you can see, while waiting, the web server wastes resources but does nothing.

When executing a request, server resources are consumed: memory, processor time, and the number of cores. So, with a multithreaded approach, the server can only execute one request at a time? Not. Each request is placed on a separate thread. A thread is the time and resources that the server allocates to execute a small block of instructions. Thus, the number of threads depends on the power of the machine. The more resources, the more threads and the more requests the server can execute simultaneously. This model of operation is called the thread-per-request model.

If free streams run out, the server cannot complete the task. It enters the queue and waits until one of the threads is free. For example, you need to execute 3 requests. 4 streams are available to you. Server power allows you to complete them all in one go. If you need to execute 5 requests, then one task will not fit and it will have to wait for a free thread. Due to the multiple threads that process requests, the system is called multichannel.

How does Node.js work? Node.js works on a single-threaded basis as react.js. With this approach, all requests are made within a single thread and the web server does not waste time waiting for a response. It sends requests and processes responses as they come in. In this way, the web server is always running and not waiting. This method allows you to efficiently use resources and execute commands much faster.

Single-threaded system

A great example is loading a website page. Imagine you want to load a website page. The web resource can appear to the user as soon as the browser has received all the required content. When using a regular web server with a multithreaded system, each file (picture, title, body text, background) will be requested separately. For example, first the background, then as soon as the background data is received, the web server will send a request for pictures, and so on. Due to the long wait for a response, the page will take longer to load. As we said, Node.js doesn’t waste time waiting. When searching for a background image, the node will ask for another image. Due to this, the site will load faster.