Author: YunFeng Github: github.com/ihtml5

The original link

🔶 profile

Imagine a Web application sending hundreds of requests per second to a server that processes one request at a time and then moves on to another. You may encounter performance issues and provide a poor experience for your users.

With this background in mind, Node.js was created in 2009 by Web developer Ryan Dahl, who encountered some difficulties in developing features with dense input and output streams using synchronous programming and multi-threaded server side platforms.

As such, Node.js is a technology built to solve a specific problem: processing intensive asynchronous input and output events. And what programming language has this capability? The answer is: JavaScript.

So in this article, I’m going to share my knowledge, experience, and research on the technology with the goal of providing the basic concepts of Node.js and helping you understand why it’s so important in the modern server-side world.

🔶 1 What is Node.js?

Simply put, Node.js is a server-side programming platform based on open source JavaScript, built on Top of Chrome’s V8 JavaScript engine. This means that the engine that compiles JavaScript in a Web browser is the same engine that runs Node.js. Core, that’s why it’s so fast, especially for Web applications.

🔶 2 Why Node.js?

Node.js is an extremely powerful server-side platform for developing modern, reliable and scalable Web applications, trusted by global companies like Netflix, Uber, LinkedIn, and PayPal.

Because of its single-threaded nature combined with a multithreaded platform running in the background, its event-driven architecture is ideal for applications with intensive I/O (input and output), enabling them to serve more clients with less hardware, which means higher performance and less cost.

In addition, the platform offers other amazing features for developers.

🔸 2.1 Non-blocking I/O

Non-blocking methods take input and asynchronously return output. In other words, it enables the server to receive many requests without blocking the application while processing the response in the background.

🔸 2.2 Single thread

Node.js can handle multiple events simultaneously, and its single-threaded nature delegates asynchronous operations to multithreaded platforms, meaning that only one thread can handle input and output.

This feature makes it lightweight, scalable, and efficient by consuming low memory and infrastructure capacity to handle large numbers of requests.

🔸 2.3 Event-driven

Node.js is an event-driven technology, meaning that the control flow of the server platform is driven by the occurrence of events. Therefore, an Event listener named Event Loop starts waiting for events the moment the Node application starts and does not stop until the application closes.

In other words, it’s like having a very efficient waiter waiting for orders before the restaurant closes.

🔸 2.4 Node package manager

Node Package Manager is the world’s largest free open source library of features that can be easily imported and used in any Node application. All of this means that whatever tools your application needs can be found in the node package manager.

🔸 2.5 No buffering

Node.js applications never buffer data, which greatly reduces the processing time for uploading files, such as video or audio. In other words, it simply outputs data in blocks, which means, for example, that users can watch videos without any distractions.

🔸 2.6 Extensible

Scalability is a core feature of Node because its architecture runs on a single thread and, combined with a multi-threaded platform, allows it to receive thousands of simultaneous events and thus handle large numbers of requests asynchronously with a low infrastructure.

🔶 3 Node.js architecture

The Node.js architecture has three key elements that work together like event processing factories. So, to make it easier to understand how these elements interact with each other, I’ll give an example of a REST API developed in Node.js.

Suppose a client sends several requests to the application:

🔸 3.1 Event queue

1- Once these requests reach the application, they enter the Event queue, which is a queue in which all events that occur in the application are ranked first and wait to be sent to the main thread called events for processing rings.

🔸 3.2 Event Loop

2- When a request (blocking action) enters the event loop, it is a single line platform that runs the V8 engine at its core to compile JavaScript, and it is delegated to the thread pool platform to handle in the background. Thus, through this architectural flow, the main thread can again be used to process other events.

🔸 3.3 Thread pools

3- in the thread pool, this is a multi-threaded platform that runs a library called libuv, which is C++ at its core, and the request (blocking action) is processed asynchronously in the background until it is completed and ready to be returned.

Conclusion 🔶

I hope this short article has helped you better understand not only how Node.js works, but also why it is a rapidly growing platform and is being used by so many companies and startups today.

In a modern and hyper-connected world, a technology that can scale quickly with a low infrastructure is certainly an important player to watch.