Ch 1: What Is Node.js and Why Was It Created?
The Problem That Changed JavaScript Forever
Let's travel back to around 2008.
At that time, JavaScript had a very clear job.
It lived inside browsers.
Developers used it for things like:
Form validation
Button clicks
Image sliders
Dropdown menus
Simple animations
A typical workflow looked like this:
HTML → Structure
CSS → Design
JavaScript → Browser Interactions
Everything seemed fine.
But as websites became more advanced, a major problem started appearing.
Developers were writing two completely different languages.
Example:
Frontend
JavaScript
Backend
PHP
Java
C#
Python
Ruby
A frontend developer who knew JavaScript often had to learn an entirely different language just to build APIs or connect databases.
Imagine running a restaurant where:
Front Counter Staff
speak English
Kitchen Staff
speak Japanese
Communication becomes difficult.
Training becomes harder.
Development becomes slower.
The software industry faced a similar challenge.
Then someone asked a question that changed everything:
"Why can't JavaScript run outside the browser?"
That question eventually led to the creation of Node.js.
And the web development world has never been the same since.
Before Node.js Existed
Imagine a simple website.
A user clicks:
Login
The request goes to a backend server.
The backend might be written in:
PHP
Java
Python
Ruby
JavaScript wasn't involved.
It simply handled browser interactions.
Many developers accepted this limitation.
But one developer wasn't satisfied.
His name was:
Ryan Dahl
Meet Ryan Dahl
Around 2009, Ryan Dahl noticed a problem with traditional web servers.
Most servers handled requests using a blocking approach.
Let's understand what that means.
Imagine a restaurant with one waiter.
A customer says:
Can you bring me a pizza?
The waiter goes to the kitchen.
Then stands there.
And waits.
And waits.
And waits.
Only after the pizza is ready does the waiter return.
Meanwhile:
Other customers cannot place orders.
This is inefficient.
Many servers behaved similarly.
They would wait for tasks to finish before handling new requests.
Ryan Dahl believed there was a better way.
The Restaurant That Inspired Modern Backend Development
Imagine another restaurant.
This restaurant has a smarter waiter.
Customer 1 orders pizza.
Instead of waiting:
Take Order
Send To Kitchen
Move To Next Customer
Customer 2 orders pasta.
Again:
Take Order
Send To Kitchen
Move To Next Customer
The waiter remains productive.
The kitchen works in the background.
This idea sounds familiar, doesn't it?
It should.
It's very similar to the Event Loop we learned in JavaScript.
Ryan Dahl realized:
JavaScript already has a fantastic model for handling asynchronous operations.
So he decided to bring that model to the server side.
That project became Node.js.
What Exactly Is Node.js?
One of the biggest beginner mistakes is thinking:
Node.js = Programming Language
This is incorrect.
Node.js is NOT a programming language.
JavaScript is the language.
Node.js is a runtime environment.
Understanding Runtime Environment
Imagine buying a car engine.
An engine alone cannot transport people.
It needs:
Wheels
Fuel
Steering
Electronics
Only then can it operate.
Similarly:
JavaScript alone cannot:
Read files
Connect databases
Create servers
Access operating system resources
Browsers provide some capabilities.
Node.js provides many more.
A runtime environment gives JavaScript the tools it needs to run outside a browser.
Simple Definition
A beginner-friendly definition:
Node.js is a runtime environment that allows JavaScript to run outside the browser.
That's the definition most developers should remember.
Why Was Node.js Revolutionary?
For the first time, developers could use:
JavaScript
for:
Frontend
Browser Development
and
Backend
Server Development
Using one language across the entire application became possible.
This was a huge productivity boost.
Real World Example
Imagine building the AQAD marketplace.
Frontend:
React
React Native
JavaScript
Backend:
Node.js
JavaScript
Now:
Same language
Similar syntax
Shared logic
Easier hiring
Faster development
This is one reason Node.js became so popular.
What Can Node.js Do?
Once Node.js arrived, JavaScript gained new powers.
For example:
Create Servers
Handle HTTP Requests
Handle APIs
Handle Authentication
Read Files
PDF Files
Images
CSV Files
Excel Files
Connect Databases
MySQL
PostgreSQL
MongoDB
DynamoDB
Handle Authentication
Login
Signup
JWT
Sessions
Build APIs
REST APIs
GraphQL APIs
Microservices
Real-Time Applications
Chat Applications
Notifications
Live Tracking
All using JavaScript.
Why Companies Love Node.js
Many large companies use Node.js.
Examples include:
Netflix
PayPal
LinkedIn
Uber
Not because it's trendy.
Because it solves real business problems.
The V8 Engine Connection
Remember our browser chapter from the JavaScript pillar?
We talked about:
V8 JavaScript engine
The JavaScript engine created by:
Node.js uses V8 internally.
This means:
JavaScript Code
↓
V8 Engine
↓
Machine Code
Node.js didn't need to create a new JavaScript engine.
It reused one of the fastest engines already available.
This helped Node.js gain popularity very quickly.
Browser JavaScript vs Node.js JavaScript
Many beginners assume they're identical.
Not exactly.
Let's compare.
Browser JavaScript
Can access:
DOM
Window Object
Local Storage
Browser APIs
Example:
document.getElementById("title");
Works in browsers.
Node.js
Can access:
File System
Operating System
Network Connections
Server Resources
Example:
const fs = require("fs");
Works in Node.js.
Important Interview Question
Will this work in Node.js?
document.getElementById("title");
Answer: No
Because: document
belongs to browsers.
Not Node.js.
This is a very common interview question.
How Node.js Handles Thousands of Requests
Imagine a traditional restaurant.
One waiter serves one customer at a time.
Now imagine a smart restaurant.
The waiter:
Accepts Orders
Delegates Tasks
Continues Working
This is similar to Node.js.
Node.js uses:
Event Loop
Non-Blocking I/O
Asynchronous Operations
to handle many requests efficiently.
We'll explore this deeply in upcoming chapters.
For now, remember:
Node.js spends very little time waiting.
That's one of its biggest strengths.
Real AQAD Marketplace Example
Imagine 5,000 retailers opening the app.
Some actions:
Login
View Products
Place Orders
Track Deliveries
Generate Reports
Node.js can efficiently manage these operations because it doesn't waste time waiting for slow tasks.
Instead:
Request Received
↓
Task Delegated
↓
Continue Handling Other Requests
This architecture is one reason Node.js became popular for APIs and marketplace platforms.
Try It Yourself
Open your terminal.
Run:
node
You should see: >
Now type: console.log("Hello Node");
Output: Hello Node
Congratulations.
You've just executed JavaScript outside the browser.
That's exactly what Node.js makes possible.
Mini Exercise
Answer these questions before moving on:
Question 1
Is Node.js a programming language?
Question 2
What engine does Node.js use?
Question 3
Can Node.js access browser DOM APIs?
Question 4
Why was Node.js created?
Question 5
What is the biggest advantage of using JavaScript on both frontend and backend?
Try answering without looking above.
If you can answer them, you've understood the chapter.
Common Beginner Mistakes
Mistake 1
Thinking Node.js and JavaScript are different languages.
Reality:
Language = JavaScript
Runtime = Node.js
Mistake 2
Thinking Node.js replaces browsers.
It doesn't.
Node.js and browsers solve different problems.
Mistake 3
Assuming browser APIs work in Node.js.
Example:
document.getElementById();
This won't work.
Mistake 4
Thinking Node.js is only for APIs.
Node.js can also power:
Automation
CLI Tools
Real-Time Apps
Scripts
Microservices
Build Tools
FAQ
Do I need JavaScript before learning Node.js?
Yes.
Node.js is much easier once JavaScript fundamentals are strong.
That's why we completed the JavaScript pillar first.
Is Node.js frontend or backend?
Primarily backend.
However, it is also used for tooling and automation.
Is Node.js difficult?
Not really.
If you understand:
Functions
Objects
Async Await
Promises
you're already well prepared.
Is Node.js good for jobs?
Absolutely.
Node.js remains one of the most in-demand backend technologies worldwide.
How Node.js Works Internally — What Actually Happens When You Run node app.js
The Command Every Node.js Developer Uses
At some point, every Node.js developer types:
node app.js
and presses Enter.
The application starts.
The server begins running.
Requests start arriving.
Everything works.
But have you ever wondered:
What actually happens after pressing Enter?
Most developers use Node.js for months or even years without fully understanding what happens behind the scenes.
It's a little like driving a car.
Most people know:
Press Accelerator → Car Moves
Press Brake → Car Stops
Turn Steering → Car Turns
But few understand:
Engine Combustion
Transmission Systems
Fuel Injection
Electrical Components
Similarly, many developers know:
node app.js
starts an application.
But they don't know what happens internally. we're going to open the black box.
By the end, you'll understand:
How Node.js starts
How V8 executes JavaScript
How Node.js talks to the operating system
Why Node.js is fast
How Node.js handles thousands of users
And you'll understand it using simple examples rather than complicated computer science terminology.
The Restaurant Reopens
Let's continue with our restaurant analogy.
Imagine:
Restaurant Owner = You
Restaurant Building = Operating System
Head Waiter = Node.js
Kitchen = Operating System Services
Order Processing Machine = V8 Engine
When you type:
node app.js
it's similar to opening the restaurant for business.
Immediately several things start happening behind the scenes.
Step 1: The Operating System Starts Node.js
When you run:
node app.js
your operating system receives the command.
Examples:
Windows
Linux
macOS
The operating system finds:
Node.js Executable
and launches it.
Think:
You Open Restaurant
↓
Owner Arrives
↓
Restaurant Opens
Node.js is now running.
But your JavaScript code hasn't executed yet.
Step 2: Node.js Starts the V8 Engine
Remember from the JavaScript pillar:
Node.js does not understand JavaScript directly.
Instead, it relies on:
V8 JavaScript engine
created by: Google Chrome Team
The V8 engine's job is simple:
JavaScript
↓
Machine Code
↓
CPU Execution
Computers do not understand:
console.log("Hello");
Computers understand:
Binary Instructions
Machine Operations
CPU Commands
V8 performs the translation.
Why V8 Was Revolutionary
Before V8 existed, JavaScript engines were slower.
Many engines:
Read Code
↓
Interpret Line By Line
V8 introduced:
Just-In-Time Compilation (JIT)
Instead of interpreting repeatedly:
JavaScript
↓
Machine Code
↓
Execute Faster
This dramatically improved performance.
One reason Node.js became popular is because it inherited V8's speed.
Step 3: V8 Reads Your File
Suppose:
console.log("AQAD Marketplace");
exists inside:
app.js
When Node.js starts:
V8 reads the file.
Think:
Restaurant Receives Menu
Before serving customers:
The staff must understand the menu.
Similarly:
V8 first analyzes the code.
Step 4: Memory Creation Phase
Before executing code:
JavaScript creates memory.
You may remember this from:
Execution Context
in Pillar Article 1.
Example:
const company = "AQAD";
function greet() {
console.log(company);
}
Before execution:
JavaScript allocates memory for:
company
greet()
This preparation phase happens first.
Step 5: Execution Begins
After memory preparation:
JavaScript starts running code.
Example:
console.log("Start");
Output:
Start
Every line is processed.
Every function call enters the Call Stack.
This part works almost exactly like JavaScript inside the browser.
But Node.js Does Something Extra
Browsers provide:
DOM
Window
Local Storage
Node.js provides:
File System
Networking
Process Management
Operating System Access
These capabilities come from Node.js itself.
Not JavaScript.
This distinction is important.
Understanding Node.js Core Modules
Imagine the restaurant already contains:
Kitchen
Tables
Billing System
Storage Room
You don't need to build them.
They already exist.
Similarly, Node.js includes built-in modules.
Examples:
fs
http
path
os
events
These modules allow JavaScript to perform backend tasks.
Example: Reading Files
Browser JavaScript:
const file = readFile("users.txt");
Not possible.
Node.js:
const fs = require("fs");
Now you can:
fs.readFile(...);
Why?
Because Node.js provides access to the file system.
What Happens During a Database Query?
Imagine:
getProductsFromDatabase();
takes:
2 Seconds
Question:
Should Node.js stop everything and wait?
If it did:
Performance would be terrible.
Instead:
Node.js uses its most important feature.
Non-Blocking I/O
This term sounds complicated.
But the idea is simple.
Let's revisit the restaurant.
Blocking Waiter
Customer says:
Bring Pizza
Waiter:
Wait 20 Minutes
Do Nothing Else
Bad service.
Non-Blocking Waiter
Customer says:
Bring Pizza
Waiter:
Send Order To Kitchen
Serve Other Customers
Excellent service.
Node.js behaves like the second waiter.
What Is I/O?
I/O means:
Input / Output
Examples:
Input:
Database Query
File Reading
API Request
Output:
Response Sent
File Written
Data Returned
Most backend operations involve I/O.
And I/O is usually slow.
Node.js avoids waiting whenever possible.
Real AQAD Example
Retailer opens the app.
Request:
Get Product Catalog
Database takes:
500ms
Node.js doesn't wait.
Instead:
Send Query
↓
Continue Processing Other Requests
↓
Receive Results Later
This dramatically improves scalability.
The Secret Weapon: Event Loop
You already learned the Event Loop in JavaScript.
Node.js relies on it heavily.
Think:
Call Stack
↓
Background Operations
↓
Callback Queue
↓
Event Loop
The Event Loop is one reason Node.js can handle many connections efficiently.
Visualizing Node.js Architecture
User Request
↓
Node.js
↓
Call Stack
↓
Event Loop
↓
Operating System
↓
Database/File/API
↓
Result Returned
↓
Response Sent
This cycle happens continuously.
Thousands of times per second.
Why Node.js Is Single Threaded
This surprises many beginners.
Node.js primarily runs JavaScript using:
One Main Thread
At first this sounds bad.
People ask:
"How can one thread handle thousands of users?"
The answer is:
It doesn't spend time waiting.
That's the key.
Traditional Approach
Imagine:
100 Customers
100 Waiters
Expensive.
Resource heavy.
Hard to manage.
Node.js Approach
Imagine:
1 Smart Waiter
Many Kitchen Workers
The waiter coordinates everything.
The kitchen performs long-running tasks.
This is closer to how Node.js operates.
Understanding libuv
Now we meet a component many developers hear about but rarely understand.
libuv
Think of libuv as:
Operations Manager
inside the restaurant.
Its responsibilities include:
File Operations
Networking
Timers
Thread Pool
Event Loop Support
Node.js relies heavily on libuv.
Without it:
Many asynchronous features wouldn't work.
Real Example
Suppose:
fs.readFile("products.csv");
is executed.
Question:
Does JavaScript read the file itself?
No.
Instead:
JavaScript
↓
Node.js
↓
libuv
↓
Operating System
↓
File Read
↓
Callback Returned
This delegation system is incredibly important.
Why Developers Love Node.js
Because it excels at:
API Servers
REST APIs
GraphQL APIs
Microservices
Real-Time Systems
Chat Applications
Live Notifications
Tracking Systems
Marketplace Platforms
Examples:
Vendor Systems
Retailer Systems
Order Management
Inventory Platforms
Very similar to AQAD.
Try It Yourself
Create:
console.log("Node Started");
setTimeout(() => {
console.log("Timer Complete");
}, 2000);
console.log("Server Running");
Predict the output before running it.
You should already know the answer if you understood the Event Loop chapter.
Expected:
Node Started
Server Running
Timer Complete
Mini Exercise
Answer these without looking back.
Question 1
What happens first when you run:
node app.js
Question 2
What is the role of V8?
Question 3
What is Non-Blocking I/O?
Question 4
Why can Node.js handle many requests efficiently?
Question 5
What does libuv do?
Common Beginner Mistakes
Mistake 1
Thinking Node.js is the V8 engine.
Reality:
Node.js
+
V8
+
libuv
+
Core Modules
Together create the runtime.
Mistake 2
Thinking Node.js is multi-threaded JavaScript.
Your JavaScript code primarily runs on:
Single Main Thread
Mistake 3
Ignoring asynchronous behavior.
Understanding:
Event Loop
Promises
Async Await
is critical for Node.js development.
Mistake 4
Thinking Node.js is always faster.
Node.js shines for:
I/O Heavy Applications
Not necessarily:
CPU Heavy Calculations
We'll discuss this later.
FAQ
Does Node.js use JavaScript?
Yes.
Node.js executes JavaScript outside the browser.
Is V8 part of Node.js?
Node.js uses V8 internally.
V8 performs JavaScript execution.
What is libuv?
libuv powers asynchronous operations and Event Loop functionality.
Why is Node.js fast?
Because it uses:
V8
Event Loop
Non-Blocking I/O
Efficient resource usage
Can Node.js handle thousands of users?
Yes.
This is one of its biggest strengths.
The V8 Engine Explained — How JavaScript Becomes Machine Code
The Translator That Makes JavaScript Possible
Imagine you're visiting a factory in another country.
The workers inside the factory understand only one language.
Let's say they understand:
Machine Language
But you speak:
JavaScript
You walk into the factory and say:
console.log("Hello World");
The workers stare at you.
They have absolutely no idea what you're saying.
Not because they're unintelligent.
But because they speak a completely different language.
Computers work exactly the same way.
Developers write:
const company = "AQAD";
But CPUs understand:
Binary Instructions
Machine Code
Low-Level Operations
The CPU cannot directly understand JavaScript.
So who performs the translation?
The answer is: V8 Engine
Without V8, Node.js wouldn't exist.
Without V8, JavaScript would never become machine instructions.
V8 acts like an incredibly fast translator between developers and computers.
What Is V8?
Simple definition:
V8 is Google's JavaScript engine that converts JavaScript code into machine code so computers can execute it.
Created by: Google
Originally for: Google Chrome
Later adopted by: Node.js
This decision changed JavaScript forever.
Why V8 Was Revolutionary
Before V8, JavaScript engines were slower.
A simplified process looked like:
Read Line
Execute Line
Read Line
Execute Line
Read Line
Execute Line
This approach is called interpretation.
It works.
But it isn't always efficient.
As applications became larger:
Developers wanted more speed.
Google responded by creating V8.
The Factory Analogy
Imagine a factory receives the same order every day.
Example:
Create 10,000 Water Bottles
A slow factory might:
Read Instructions
Build One Bottle
Read Instructions Again
Build Another Bottle
Read Instructions Again
Build Another Bottle
Very inefficient.
A smarter factory would:
Read Instructions Once
Create Efficient Process
Produce Bottles Rapidly
This is similar to how V8 works.
Understanding Compilation vs Interpretation
This topic sounds complicated.
But let's simplify it.
Interpretation
Imagine a teacher reading instructions one sentence at a time.
Example:
Step 1
Do This
Step 2
Do That
Step 3
Do Something Else
Each instruction is processed as it arrives.
This is interpretation.
Compilation
Now imagine reading the entire manual first.
Creating an optimized plan.
Then executing it efficiently.
This is compilation.
Where Does V8 Fit?
V8 combines both approaches.
It uses something called:
JIT Compilation
Which means:
Just-In-Time Compilation
This is one reason V8 is so fast.
What Does JIT Mean?
Let's return to our factory.
Imagine receiving an order.
The factory doesn't immediately build a permanent production line.
Instead:
Receive Order
Analyze Order
Create Optimized Process
Start Production
Only when necessary.
That's essentially what V8 does.
Visualizing V8
JavaScript Code
↓
V8 Engine
↓
Machine Code
↓
CPU Executes
Every Node.js application follows this path.
What Happens When You Run Code?
Example:
const company = "AQAD";
console.log(company);
Step 1:
V8 reads the code.
Step 2:
V8 analyzes syntax.
Step 3:
V8 generates internal representations.
Step 4:
V8 converts operations into machine instructions.
Step 5:
CPU executes the instructions.
Output:
AQAD
All of this happens extremely quickly.
Why Computers Need Machine Code
Imagine telling a computer:
const price = 500;
The CPU doesn't understand:
const
price
=
500
The CPU only understands low-level instructions.
Examples:
Move Data
Allocate Memory
Perform Calculation
Store Result
V8 performs the translation.
Why V8 Is Fast
There are several reasons.
Reason 1: JIT Compilation
Instead of interpreting everything repeatedly:
V8 compiles frequently used code.
Result:
Less Repetition
More Speed
Reason 2: Optimization
Imagine a warehouse worker.
At first:
Walking Slowly
Learning Layout
After a week:
Knows Exactly Where Everything Is
Works faster.
V8 behaves similarly.
It identifies patterns.
Then optimizes execution.
Reason 3: Efficient Memory Management
V8 constantly manages memory.
Unused objects are cleaned automatically.
This process is called:
Garbage Collection
We'll discuss it later.
For now, remember:
V8 doesn't like wasting memory.
Hidden Work You Never See
Suppose:
const total = 10 + 20;
Looks simple.
Behind the scenes:
V8 performs many operations.
Examples:
Allocate Memory
Store Numbers
Perform Addition
Store Result
Prepare Output
Developers rarely notice because V8 handles everything automatically.
Real AQAD Example
Imagine the marketplace receives:
const order = {
orderId: "ORD-101",
amount: 500
};
V8 must:
Create Object
Allocate Memory
Store Values
Track References
Manage Cleanup
All before the application can use the object.
This happens millions of times daily in production systems.
Understanding Memory Allocation
Consider:
const vendor = "Fresh Foods";
Question:
Where is this value stored?
Answer:
Memory.
When variables are created:
V8 allocates memory space.
Think of it like reserving a shelf in a warehouse.
Example:
Shelf A → Vendor Name
Shelf B → Product List
Shelf C → Order Data
V8 manages these shelves automatically.
The Warehouse Analogy
Imagine a warehouse.
New products arrive:
Allocate Shelf
Store Product
Track Location
When products are no longer needed:
Remove Product
Free Shelf
This is similar to memory management.
What Is Garbage Collection?
Suppose:
let product = {
name: "Pepsi"
};
Later:
product = null;
Now the object is no longer needed.
V8 eventually notices:
No References
Object Unused
Memory Can Be Reclaimed
This cleanup process is called:
Garbage Collection
Why Garbage Collection Matters
Without automatic cleanup:
Applications would eventually consume all available memory.
Imagine a restaurant that never removes:
Old Plates
Empty Bottles
Unused Tables
Eventually there would be no room left.
Garbage Collection prevents this problem.
How V8 Helps Node.js Scale
Suppose AQAD receives:
10,000 Product Requests
Every request creates:
Objects
Arrays
Functions
Variables
V8 manages all of them.
Efficient memory management helps Node.js remain fast and responsive.
Why Backend Developers Should Care
Many beginners think:
"V8 is internal. Why should I learn it?"
Because understanding V8 helps explain:
Performance issues
Memory leaks
Optimization techniques
Application scaling
Garbage Collection behavior
You don't need to become a V8 engineer.
But understanding the basics makes you a stronger backend developer.
Try It Yourself
Create:
const company = "AQAD";
const users = [];
for(let i = 0; i < 5; i++) {
users.push(i);
}
console.log(users);
Ask yourself:
How many objects and values must V8 allocate memory for?
Thinking this way improves your understanding of runtime behavior.
Mini Exercise
Question 1
What is V8?
Question 2
Who created V8?
Question 3
What is the primary role of V8?
Question 4
What does JIT stand for?
Question 5
What is Garbage Collection?
Try answering before reading the summary.
Common Beginner Mistakes
Mistake 1
Thinking V8 is Node.js
Reality:
Node.js
uses
V8
They are not the same thing.
Mistake 2
Ignoring Memory Usage
Creating objects is easy.
Managing memory efficiently is harder.
Large applications must consider memory consumption.
Mistake 3
Thinking JavaScript Runs Directly on the CPU
It doesn't.
The path is:
JavaScript
↓
V8
↓
Machine Code
↓
CPU
Mistake 4
Believing Garbage Collection Solves Every Memory Problem
Memory leaks can still happen.
We'll discuss them in later chapters.
FAQ
Is V8 a browser?
No.
V8 is a JavaScript engine.
Does Node.js use V8?
Yes.
V8 executes JavaScript inside Node.js.
Is V8 written in JavaScript?
No.
V8 is primarily written in C++.
Why is V8 fast?
Because of:
JIT Compilation
Optimization Techniques
Efficient Memory Management
Do backend developers need to know V8?
Yes.
At least at a conceptual level.
It helps explain how Node.js works internally.
Next Chapter
Ch: The Node.js Event Loop — The Secret Behind Handling Thousands of Requests
In the next chapter, we'll dive deeper into one of Node.js's most important concepts:
Event Loop Internals
Phases of the Event Loop
Timers
Poll Queue
Check Phase
setTimeout vs setImmediate
process.nextTick
Real-world API behavior
We'll use a large restaurant and order-management analogy so that even advanced Event Loop concepts remain simple and memorable.

0 Comments