Introduction:
Ever wondered how your favorite web apps, like social media or streaming platforms, handle thousands of users at the same time? That's where Node.js comes into play.
Now, when we think of web applications, we imagine them running in a browser. And every browser natively understands only HTML, CSS and JavaScript. So in this case JavaScript executes in browser. Means browser is the JavaScript runtime.
But what if we could take JavaScript out of the browser and simply execute our JavaScript code outside the browser without any restrictions of browser.
What is Node.js ?
Node.js is a single threaded JavaScript runtime environment built on Google’s open source V8 engine. Means we are executing JavaScript outside the browser without any restrictions🥳.
You can think it like a container or environment in which program written in JavaScript can be executed, but outside on any browser whatsoever.
Now you have a question that, if not the browser who is executing the code🤔.Here the Google’s V8 engine🌐comes into picture and it executes the JavaScript Code.
Now, We have the JavaScript outside the web browser means in standalone environment which is just a Node.js. Hence we can do so many things that are completely impossible 🤯 before, like accessing the file system or better networking capabilities so all this factors together give us the perfect conditions for using Node.js as a web server. Meaning that we can finally use JavaScript on the server side of web development in order to build fast, highly scalable network applications for powering the backend for all websites or web-applications.
Why 🚀and When 🤔to use Node.js
Node.js processes multiple requests simultaneously, making it fast and efficient for real-time applications.
Ideal for applications like streaming platforms, where high performance and quick responses are critical.
Node.js allows you to use JavaScript for both frontend and backend development, simplifying the development process.
Node.js has an extensive package ecosystem, enabling developers to easily add features and functionality.
Node.js works well with NoSQL databases like MongoDB, making it great for modern applications.
Perfect for streaming platforms (e.g., YouTube), as Node.js can handle data streams efficiently.
Node.js excels in real-time applications, such as chat apps, thanks to its event-driven architecture.
Node.js is ideal for building dynamic, scalable web applications on the server side.
When Not to Use Node.js:
Node.js isn't ideal for applications with heavy server-side processing, such as CPU-intensive operations, where other technologies like Python, PHP, or Ruby on Rails may be better suited.
Node.js struggles with CPU-intensive tasks—those that require a lot of computing power—because of its single-threaded, event-driven nature.
for example :
Imagine a fast-food restaurant where there's only one cook (Node.js). If the cook is busy making a complex, multi-layer cake (CPU-intensive task), everyone else waiting for simple orders (like coffee or fries) will have to wait until the cake is finished. This slows down the whole system.
Node Architecture Behind the scene's🚀:
There are couple of libraries that Node depends on in order to work properly. So the node runtime have the several dependencies and most important ones are V8 Engine and libuv .
V8 Engine :
We discussed earlier that Node is a built on Google’s open source V8 engine.
so that’s why it appear here as dependency. If it wasn’t for V8 Node would have absolutely no way of understanding the JavaScript code that we write. Hence V8 is the fundamental part in Node architecture. So the V8 engine is what which converts JavaScript code to machine code that computer can actually understand. So we can say that V8 engine is the heart of Node.js.
libuv : V8 engine is not enough to create a whole server side framework like Node, And so that is why we also have Iibuv in Node. A Iibuv is an open source library with a strong focus on asynchronous IO (input output). This layer is what gives Node access to the underlying computer operating system, file system, networking, and more. Besides that, Iibuv also implements two extremely important features of Node.JS which are the event loop and also the thread pool. And in simple terms, the event loop is responsible for handling easy tasks like executing call backs and network IO while the thread pool is for more heavy work like file access or compression or something like that.
Note : V8 is written in JavaScript and C++. libuv is completely written in C++ and not in JavaScript. Hence Node itself a program that is written in JavaScript and C++. but it allows us to write 💯% of JavaScript code and that the beauty of Node❤️.
Node does not actually rely on V8 and libuv but also on
http-parser : For parsing http.
The
http-parser
is used by Node.js to handle HTTP requests and responses. It interprets the incoming HTTP message and extracts useful data like headers, URL, and method (GET, POST, etc).c-ares : For some DNS request stuff.
c-ares
handles DNS resolution in Node.js. It resolves domain names (likewww.google.com
) to IP addresses without blocking the event loop.OpenSSL : For cryptography.
Node.js uses OpenSSL for encryption, decryption, and secure communications. It handles tasks like generating SSL certificates, hashing data, and managing secure HTTPS connections.
zlib : For compression.
Node.js uses
zlib
for data compression to reduce the size of files or responses, speeding up data transfer and reducing bandwidth usage.
When we have all these pieces nicely fit together, we end up with Node.JS ready to be used🔥🥳.
How Node Actually Work🔥🚀:
Understanding Node.js Process and Thread Pool
To understand the internal working of Node first we have to take information about Node.js processes and Thread pool.
Node.js Process:
Initialize Program: This is where the program starts, executing a sequence of instructions.
Execute ‘Top-Level’ Code: Node.js runs the main code of your application.
Require Modules: It loads the necessary modules (libraries) your application needs.
Register Event Callbacks: It sets up functions to handle events (like user inputs or network requests).
Start Event Loop:
- This is the core of Node.js’s non-blocking I/O model. It continuously checks for and processes events, allowing Node.js to handle multiple operations efficiently.
Thread Pool:
Node.js uses a thread pool to offload heavy tasks from the event loop. This includes:
File System APIs: Reading and writing files.
Cryptography: Encrypting and decrypting data.
DNS Lookups: Resolving domain names.
Compression: Compressing and decompressing files.