Basic Concepts of Node.js
Tags: architecture, node, webdev, javascript
Published: 2021-08-25 11:50:42

## 🔶 Introduction

Imagine a web application which sends hundreds of requests per second to a server that only processes one request per time before moving to another request. Probably you will have some performance problems and provide a bad experience for your users.

With this context in mind, Node.js was created in 2009 by Ryan Dahl, a web developer, after he faced some difficult issues by developing features with intense input and output flow using synchronous programming and multi-threaded server-side platforms.

Therefore, **Node.js was a technology built to solve a specific problem: deal with intense asynchronous input and output events**. And, which programming language has this capacity? The answer was: JavaScript.

So, in this article, I share my knowledge, experience and studies about this technology, with the goal to provide the basic concepts of Node.js, and help you to understand why it’s such an important player in a modern server-side world.

## 🔶 1 What is Node.js?

In simple terms, **Node.js is an open-source JavaScript based platform for server-side programming built on Chrome's V8 JavaScript engine**, which means that the engine that compiles JavaScript in a web Browser it's the same engine that runs Node.js in it’s core, and this is what makes it so fast, especially for web applications.

## 🔶 2 Why Node.js?

**Node.js is an extremely powerful server-side platform to develop modern, reliable and scalable web applications**, trusted by global companies such as Netflix, Uber, LinkedIn and PayPal.

Due to its single thread characteristics combined with a multi thread platform that runs in background, **it’s event driven architecture is perfect for applications with intense I/O (Inputs and Outputs)**, making it serve more clients with less hardware, which means more performance with less costs.

Besides that, this platform provides other amazing features for developers.

![Node.js Advantages](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x1km68oj19u8otbknnqk.jpg)

### 🔸 2.1 Non-Blocking I/O

Non-blocking method receives an input and returns the output asynchronously. In other words, **it makes it possible for a server to receive many requests without blocking the application while the response is being processed in the background**.

### 🔸 2.2 Single Thread

**Node.js can deal with many events at the same time with its single thread characteristic that delegates the asynchronous operations to a multi thread platform**, which means that **just one thread is able to handle inputs and outputs**.

This feature makes it lightweight, scalable and efficient by consuming low memory and infrastructure capacity in order to process a huge number of requests.

### 🔸 2.3 Event Driven

Node.js is an event driven technology, which means that **the control flow of this server side platform is driven by the occurrence of events**. So, at the moment that a Node application starts, an event listener called Event Loop begins to wait for events and doesn't stop until the application is shutted down.

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

![Node.js Event Driven](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezpocyug0okbj7dodf50.jpg)

### 🔸 2.4 Node Package Manager

Node Package Manager it’s **the world's largest free and open source library of functionalities, and can be easily imported and used in any Node application**. All this means that, regardless of the tools that an application needs, it will be found at Node Package Manager.

### 🔸 2.5 No Buffering

Node.js applications never buffer data, which dramatically reduces the processing time of uploading files, such as videos or audios. In other words, it **simply outputs data in chunks**, meaning that, for example, a user can watch videos without any interruption.

### 🔸 2.6 Scalable

Scalability is a core Node characteristic, due to a **high capacity of handling large amounts of requests asynchronously with low infrastructure** by it’s architecture that operates on single thread combined with multi thread platform, allowing it receives thousands of simultaneous events.

## 🔶 3 Node.js Architecture

**Node.js architecture has 3 key elements that work together like an event processing factory**. So, in order to make it easier to understand how these elements interact with each other, I will give an example of a REST API developed in Node.js.

Let’s say that a Client sends several requests to this application:

### 🔸 3.1 Event Queue

1- As soon as these requests reach the application, they go to the Event Queue, which is a **queue where all the events that occur in the application goes first**, and where they await to be sended to be processed in the main thread called Event Loop.

### 🔸 3.2 Event Loop

2- When a request (Blocking Operation) enters in the Event Loop, which is a **single thread platform that runs the V8 Engine in its core to compile JavaScript**, it’s delegated to the Thread Pool platform to be processed in background. So, with this architectural flow, the main thread is available again to handle other events.

### 🔸 3.3 Thread Pool

3- In the Thread Pool, which is a **multi thread platform that runs a library called libuv and has C++ in its core**, the request (Blocking Operation) is processed asynchronously in the background until it’s completed and ready to be returned.

![Node.js Architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vkhqcx31nngbl2kfuj1s.jpg)

## 🔶 Conclusion

I hope that this brief article helped you to have a better understanding of not just how Node.js works, but also why it’s a platform that growed quickly and it's used in so many companies and startups nowadays.

In a modern and ultra-connected world, a technology that is able to scale fast with low infrastructure for sure it’s an important player to pay attention to.

So, if you arrived here, **I invite you to read my another article about the fundamentals of tools that are also essential in the web world that we live in: API, REST and HTTP Protocol.**

[Fundamentals of REST API](https://dev.to/cassiocappellari/fundamentals-of-rest-api-2nag)

*Written with 💙 by Cássio Cappellari!*