Node.js in Simple Terms

Hey folks, many of us learn building backend projects directly and I completely agree that one is able to learn with this approach, but the issue is if you simply ask “what is node.js”, they will be stuck. Basically, they are comfortable building things that too production grade but they don’t have clear image of how to tackle fundamental questions about the tech they are using. So, let’s dive and try to understand it.
What is Node.js?
Node.js® is a free, open-source, cross-platform JavaScript runtime environment that lets developers create servers, web apps, command line tools and scripts.
This is the definition you get on official node.js site.
First try to understand the terms here then we will again see in easy language.
Open source - Its source code is public (https://github.com/nodejs/node)
Cross Platform - The same code can be run on different OS.
Runtime env - It is the software layer that provides everything needed to run a program written in a specific programming language.
So, little more detailed definition
Node.js is a open source, cross platform, JS runtime environment that allows developers to run JavaScript outside the browser built on Chrome’s V8 engine. It uses single threaded, non-blocking I/O model (asynchronous nature) with access to system resources like file system, network and OS for building fast, scalable, real time applications.
Components
V8 engine - V8 engine by Google written in C++ used in Chrome to compile JS to machine code using Just-In-Time (JIT) compilation.
libuv (Event loop + Thread pool) - It is multiplatform C library to basically perform async I/O using single threaded Event Loop and Thread Pool. (both of these are important topic of discussion themselves)
Node APIs (built-in modules)
fs (file system)
https (create web server)
os, crypto, events
Why Node.js over other backend runtimes like Python, Java or PHP?
Async I/O for real time apps like chat, notifications, or live updates.
Unified language (JavaScript) access frontend and backend reduces context-switching for developers.
Huge npm ecosystem for third party packages.
Faster execution with the help of V8 engine.
Perfect for API driven, microservice-based, or event driven architecture.
Async Work (Call stack, Event Loop, Thread Pool)
It is a topic to understand in itself, here is just an overview of the bigger picture.
Single threaded JS engine(V8) with a Call Stack (eventually every function is run by this call stack only and we only have one call stack)
An Event Loop coordinates tasks.
Thread Pool via libuv to handle expensive I/O operations in threads.
Multiple task queues (Timer queue, I/O queue, Microtask queue etc)
We can understand Event Loop and Thread Pool with an simple analogy.
Event Loop: The traffic cop
Checks call stack empty?
Are there any callbacks in the queue to run
Queues into the Call Stack
Thread Pool (libuv) - Worker team
Expensive task such as fs.readfile, DNS lookups, computations, crypto hashing etc are handled by Thread Pool. (by default we have 4 threads which can run simultaneously)
Thread Pool
Callback Queue into I/O queue.
Event loop picks.
Pushes to Call Stack
Thank you for reading.
Subscribe to my newsletter
Read articles from Maneesh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
