Basic concepts of Node.js
Node.js is a great technology aimed to process numerous asynchronous inputs and outputs, aiming to improve the application’s performance and the user experience.
This article will provide the basic concepts of Node.js, proving its importance in the modern world of server-side development.
What is Node.js?
Reasons to choose Node.js
In the light of its single-thread characteristics and the background-running multi-thread platform, its event-driven architecture suits best applications with intense inputs and outputs ratio (I/O). Thus, it is perfect for performing more while spending less.
In addition to that, the platform provides even more great features for developers.
Non-blocking inputs and outputs
This means that the method is capable of receiving input and giving back output asynchronously. This means that the server may receive numerous requests and process the responses without blocking the application from running properly.
A single thread handles all inputs and outputs, making the platform lightweight and scalable while consuming less memory.
Being event-driven means that Node.js’s flow control is run by the occurrence of events. That means that once the Node.js application has been started, an event listener called Event Loop starts waiting for events, and it won’t stop until the application is shut down.
Platform’s Package Manager
The Node Package Manager is the world’s most extensive open-source library of functionalities, and it can be easily imported to and used in any Node-written application. Thus, any tool an application might need can be found in the library.
No data buffering fastens the process of uploading files. Node.js applications output the data in chunks, which remarkably improves user experience.
Thanks to the previously mentioned single thread, the platform is highly capable of handling numerous requests asynchronously. The scalability of the platform is its core characteristic, which allows it to process thousands of events at a time.
The architecture consists of three key elements. These are Event Queue, Event Loop, and Thread Pool.
Let’s take a brief look at this using a REST API developed in Node.js as an example.
So what happens first? Let’s say that a client sends a number of requests to the application.
Once the requests reach the application, they get to the Event Queue, where they are waiting to be processed to the Event Loop. The Event Queue is an actual queue where all the application events process to the Event Loop one by one.
Thread Pool is a multi-thread platform that has C++ in its core and runs a library called libuv. At this stage, a single request is processed in the background until its ready to be returned.