Phase 1 — Foundations lesson-0003

What is Node.js?

Node.js is not a language, not a framework — it's a runtime environment that lets JavaScript run outside the browser.

The Architecture

Node.js is built on two core components that work together:

Your Code
JavaScript
The application you write
JavaScript Engine
V8
Compiles JS to machine code (built by Google for Chrome)
Async I/O + Event Loop
libuv
C library — handles file system, networking, timers, threads
Operating System
Linux / macOS / Windows
Actual system calls happen here
Key Insight
Node.js did NOT invent JavaScript. It took V8 (Google's JS engine) out of Chrome and added libuv on top, enabling JS to do things browsers never allowed — like reading files, listening on ports, and spawning processes.

The Event Loop — Simplified

Node.js runs on a single thread. This sounds like a weakness, but it's actually its superpower — here's why:

Node process starts │ ▼ ┌─────────────────────────────────────────┐ │ Event Loop │ │ │ │ 1. Execute synchronous JS code │ │ 2. Process any resolved Promises │ │ 3. Check for I/O events (file, network) │ │ 4. Run timers (setTimeout/setInterval) │ │ 5. Go back to step 1 │ └─────────────────────────────────────────┘ │ │ When you call fs.readFile(...) │ libuv hands it to the OS │ Node keeps running other code ←── THIS is non-blocking I/O │ When OS finishes, it queues a callback ▼ Callback fires when the loop reaches it

Because Node hands I/O work to the OS and continues, a single Node process can handle thousands of simultaneous connections — all waiting for databases, files, or network responses — without spawning thousands of threads.

Node.js vs Other Runtimes

Node.js is great for ✓

🌐HTTP APIs and microservices — high concurrency, low CPU
Real-time apps — chat, notifications, live dashboards
🔧Dev tooling — bundlers, CLIs, build systems
📦Streaming data — large file transfers, video pipelines

Node.js struggles with ✗

🔢CPU-intensive work — video encoding, ML inference, cryptography
🧮Heavy computation — it blocks the single thread
🐍Data science / ML — Python's ecosystem dominates here
The Golden Rule
Never block the event loop. If you run a CPU-heavy operation synchronously (like a huge loop or synchronous file read), your server stops handling all other requests until it finishes. This is the most common Node.js performance mistake.

Your First Node.js Script

Open a terminal and try this — no framework, no npm packages:

// hello.js
const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify({ message: 'Hello from Node.js!' }));
});

server.listen(3000, () => {
  console.log('Server running at http://localhost:3000');
});
# Run it
node hello.js

# In another terminal, test it
curl http://localhost:3000

This is a complete HTTP server in 8 lines. Express (which you'll learn next) just makes this pattern much more comfortable to work with at scale.

Node Version Management

Always use a version manager — never install Node.js directly. The two most popular:

  • nvm (Node Version Manager) — the classic, shell-based
  • fnm (Fast Node Manager) — newer, faster, Rust-based
# Install a specific version
nvm install 20

# Use it in the current project
nvm use 20

# Pin the version for a project (.nvmrc file)
echo "20" > .nvmrc
Which version?
Always use the LTS (Long Term Support) version for production. Even-numbered versions are LTS. At time of writing: Node 20 LTS. Odd-numbered versions (19, 21) are short-lived.

🧠 Check Your Understanding


Go Deeper

Primary source: nodejs.org — Introduction to Node.js

Video: Fireship — Node.js Explained in 100 Seconds

Ask your teacher: "What happens when you block the event loop with a big loop?" or "Show me non-blocking vs blocking I/O side by side."