1) Node Introduction
Basic Introduction
Node.js is a runtime environment that allows you to run JavaScript outside the browser, typically on the server.
It’s built on Google Chrome V8 Engine, which compiles JavaScript into fast machine code.
Node.js lets you use JavaScript to build backend services, APIs, real-time apps, and scalable network applications.
i) Installation of Node.js
a) On distributions that supportaptpackage manager, you can install Node.js with the following commands,sudo apt update sudo apt install nodejs sudo apt install npmb) Verify Installation
Open your command line tool (Command Prompt, Terminal, etc.).
Typenode -vto see the Node.js version installed.
Typenpm -vto see the npm (Node Package Manager) version installed.When to Use Node.js
Best for: REST APIs Real-time apps (chat, notifications), Streaming apps, Microservices architecture
Avoid for: Heavy CPU processing Complex multi-threaded enterprise systemsHow node js Works
Node.js uses an event-driven, non-blocking I/O model, which is very different from traditional server models.
i) Single-threaded Event Loop
Node.js runs on a single main thread
Uses an event loop to handle multiple requests efficiently
ii) Non-blocking I/O
Operations like file reading, DB calls, API requests are asynchronous
Instead of waiting, Node.js continues executing other tasks
iii) Event Loop Flow
Request comes in
If it’s a simple task → handled immediately
If it’s I/O (DB, file, API): Sent to background workers (libuv thread pool)
Node.js continues handling other requests
Once the task completes → callback/promise is executed
Here High concurrency with low resource usageAdvantages of Node.js
i) High Performance
Powered by V8 engine
Fast execution due to JIT compilation
ii) Non-blocking & Scalable
Handles thousands of concurrent connections efficiently
Ideal for: Real-time apps (chat, gaming), Streaming apps, APIs
iii) Same Language (JavaScript Everywhere)
Frontend + Backend both use JavaScript
Easier for full-stack developers
iv) Huge Ecosystem
npm provides millions of packages
v) Great for Microservices
Lightweight and fast startup Easy to scale horizontally
vi) Real-time Capabilities
Works well with WebSockets (e.g., chat apps, live updates)Limitations of Node.js
i) Not Ideal for CPU-Intensive Tasks
Since it’s single-threaded: Heavy computations block the event loop
Example: Image processing Large data calculations
Solution: Worker threads / microservices
ii) Callback Hell (historically)
Deep nested callbacks can make code messy (Though modern solutions exist: Promises, async/await)
iii) Less Mature for Heavy Enterprise Workloads
Some areas (like heavy multi-threading or complex transactions) are better handled by languages like Java
iv) Error Handling Complexity
Asynchronous code can make debugging harder if not structured properly
v) API Instability in Some Packages
Open-source ecosystem is huge, but: Some libraries are poorly maintainedV8 Engine
The Google Chrome V8 Engine (commonly called V8) is a high-performance JavaScript engine developed by Google.
Its main job: Convert JavaScript code into machine code so the computer can execute it fast.
How V8 Works (Step-by-Step)
i) Parsing
Reads JavaScript code
Converts it into an Abstract Syntax Tree (AST)
👉 Think: breaking code into a structured format
ii) Ignition (Interpreter)
V8 first uses Ignition (its interpreter)
Converts AST → Bytecode
Executes the code quickly without waiting for full optimization
👉 Fast startup time
iii) TurboFan (Compiler)
Frequently used code (“hot code”) is optimized by TurboFan
Converts bytecode → optimized machine code
👉 This is called Just-In-Time (JIT) Compilation
iii) Execution
Machine code runs directly on CPU
Optimized paths make execution very fast
iv) De-optimization
If assumptions fail (e.g., variable types change), V8:
Falls back to unoptimized code
Re-optimizes again if needed
👉 Smart but dynamic optimizationKey Concepts
i) Just-In-Time (JIT) Compilation
Combines: Interpreter (fast start) and Compiler (high performance)
ii) Hidden Classes
V8 internally optimizes objects using hidden classes
Objects with same structure → faster access
iii) Inline Caching
Caches object property access
Avoids repeated lookups → faster execution
iv) Garbage Collection
Automatically frees memory
Uses Generational Garbage Collection: Young Generation (short-lived objects) Old Generation (long-lived objects)
v) Call Stack & Heap
Call Stack → function execution
Heap → memory allocation for objects
vi) Why V8 is Fast
Compiles JS to native machine code
Uses JIT optimization
Smart memory management
Optimizes frequently used code pathsnodemon module
nodemonis a utility that helps develop Node.js applications by automatically restarting the node application when file changes in the directory are detected. It is particularly useful for development, where you might frequently change your code and need the server to reflect those changes without manually restarting it each time.
You can installnodemonglobally or as a development dependencynpm install --save-dev nodemon npm install -g nodemonAfter installing, you can run your application using
nodemoninstead ofnodenodemon your_script.js