DEV Community

Cover image for What is NodeJS?
Samira Awad
Samira Awad

Posted on • Updated on

What is NodeJS?

Node.js is an open source runtime environment for the server layer. It allows developers to create highly scalable applications with tens of thousands of simultaneous connections on a single server, thereby reducing infrastructure costs.

It is ideal for handling high traffic and event applications, such as Twitter and LinkedIn, where many requests arise per second. Node.js uses Google's V8 engine, allowing developers to write JavaScript on both the server and client sides, improving data transfer and reducing work times.

HISTORY
Before 2009, JavaScript could only be run within a web browser. Every browser has its own JavaScript engine, like V8 in Chrome, SpiderMonkey in Firefox, and Chakra in Microsoft Edge. These engines convert JavaScript code into machine code, the language that computers can understand and execute.

However, most server programs allowed a maximum of 4,000 simultaneous connections between browsers and servers, requiring more servers to increase that number.

Node.js was created in 2009 to allow JavaScript to run on the server, using the V8 engine and in a C++ program. The choice of V8 was due to its speed and ability to execute JavaScript independently of the browser.

Node.js allows you to write browser-independent JavaScript code and access additional modules, such as the ability to access system files or create a server. Unlike a browser, where access is limited to what it allows, Node.js offers greater flexibility by allowing access to system APIs, such as file manipulation and use of the device's camera.

Node.js remains asleep until activated by an event or request. Each request triggers the execution of JavaScript code without blocking other operations. It is oriented towards asynchronous events and designed to create scalable network applications, using a single thread of execution to handle multiple requests efficiently.

Node.js has its own package manager (npm), considered the largest library ecosystem in the world. npm allows you to manage dependencies, download third-party libraries and programs, and distribute code.

SYNCHRONOUS AND ASYNCHRONOUS EVENTS
In a synchronous environment, when JavaScript code is executed, CPU resources are used. If the code requires writing something to the database, the CPU is stopped and the I/O process is used, which generally blocks the code until the writing is complete. In this model, a thread must wait for one operation to complete before proceeding with the next, which can saturate available resources by creating multiple threads to handle multiple simultaneous requests.

On the other hand, Node.js, everything works by events, following an asynchronous model. When the code finishes executing, a request to write to the database begins, and the JavaScript process ends there.

While the database executes the recording, Node.js continues to handle other requests. When the data is written and the database returns a response, another event is fired that executes more JavaScript code. This system, called event loop, allows operations to not block the system, optimizing CPU usage.

Node.js was designed to work with HTTP, handle events, and stream data quickly. It works on the single-threaded principle, that is, it uses a single thread of execution to handle multiple requests efficiently. However, it allows access to multiple CPU cores and load balancing, distributing requests between different environments to optimize processing.

In an asynchronous model, a single thread of execution can handle multiple requests simultaneously without needing to wait for one operation to finish before starting another. For example, when a request arrives at the server that requires querying the database, the thread continues with the next request while the database executes the query.

When the database returns a response, it places a message in the event queue, which Node.js continually monitors and processes as they arrive.

USE CASES
Node.js is ideal for real-time applications, such as online chats and games, as well as applications that handle a lot of network operations and events, such as web servers or streaming services like Netflix.

However, it is not recommended for applications that require many CPU calculations and few input/output (I/O) operations, since all requests would have to wait for the single thread of execution to become free.

When doing calculations on the CPU, Node.js cannot delegate it to other systems like it can with database operations. An example of this is an image manipulation service.
Node.js allows isomorphic applications, that is, the same language on the frontend and backend, thus facilitating maintenance.

I hope this post has helped you :) <3

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.