Introduction
Welcome to this article where we will discuss what is node.js and few important aspects of node.js. Before node.js, javascript is only restricted to creating dynamic and fancy websites because javascript can only work inside web browsers. Why so?
The reason behind this is very simple. Javascript needs runtime environment for its code to run and before node.js only browsers have those runtime environment. With introduction of node.js, javascript can interact with system functionality which are typically inaccessible from within web browser like file systems access, networking and other system-level operations. Let's see how node.js does all this.
How Node.js have such superpowers?
Many consider node.js as programming language. But that's not true. Node.js is not a programming language but it is a runtime environment. Remember node.js provide javascript runtime environment outside web browsers. But the question arises, how? The basic and simple answer to this question is V8 engine and its binding with c++. Node.js has various key components which are backbone of node.js. Let's see what are those.
1. V8 Engine:
V8 Engine is very powerful javascript engine developed by google which is designed to execute javascript code quickly and efficiently. While v8 engine is initially developed javascript within browsers but node.js leverages this engine to run javascript code outside browser.
2. C++ binding:
Node.js extends capabilities of v8 engine by using c++ bindings. Functionalities like files system and other system functionalities can only be accessible with these bindings.
3. Libuv Library:
Libuv is a multi-platform C library that Node.js uses to handle asynchronous I/O operations. It provides the event-driven architecture that Node.js is known for, allowing it to perform non-blocking operations and handle many connections concurrently. Libuv manages several key tasks:
Event Loop: The event loop is the core of Node.js’s asynchronous programming model. It processes incoming events and executes their associated callback functions.
Asynchronous I/O: Libuv provides a consistent API for various asynchronous operations, such as file I/O, network communication, and timers.
Thread Pool: For operations that cannot be performed asynchronously (e.g., some file system operations), Libuv uses a thread pool to offload these tasks, ensuring the main event loop is not blocked.
4. Node.js core modules:
Node.js includes a set of built-in modules that provide essential functionality for building server-side applications. These modules are written in both JavaScript and C++ and expose various APIs to the JavaScript runtime. Examples include:
http: For creating HTTP servers and handling HTTP requests and responses.
fs: For interacting with the file system, allowing reading, writing, and manipulation of files and directories.
path: For handling and transforming file paths.
events: For event-driven programming with the EventEmitter class.
Node.js architecture and how does it works?
When node.js server starts several key process take place to intialize and run the server.
V8 javascript engine is initialized.
Libuv library which handles asynchronous I/O operations and event loop is initialized.
Core modules like 'fs', 'http', 'path' etc.. are loaded.
User modules and application entry point (app.js) is loaded.
Global objects and environment variables are set up.
Thread pool is initialized to handle blocking operations like file system and DNS operations.
Once server starts, event loop run continuously waiting for event to run. When event or request comes in event loop picks it up from queue and check whether it is blocking I/O operation, file system operation. It not it process the request and send response. If the request has a blocking operation to perform, the event loop assigns a thread from the internal thread pool to process the request. There are limited internal threads available. This group of auxiliary threads is called the worker group. After fulfilling the request from thread pool it goes to event loop. Event loop continuously check call stack and when no request is there in call stack, then it goes for execution.
Conclusion
Node.js provides a powerful and efficient runtime environment for executing JavaScript code outside the browser, enabling interaction with system-level operations. By leveraging the V8 engine, C++ bindings, Libuv library, and core modules, Node.js offers a robust platform for building scalable and high-performance server-side applications.