Introduction to Node.js
What Node.js is, why it exists, and where it shines.
What is Node.js?
Node.js is a JavaScript runtime — a program that runs JavaScript code outside of a web browser. Before Node.js appeared in 2009, JavaScript lived almost exclusively inside browsers, where it manipulated web pages. Node.js took Google Chrome's JavaScript engine (called V8), wrapped it with system-level capabilities like file and network access, and turned JavaScript into a general-purpose language for servers, command-line tools, and scripts.
A key mental model: Node.js is not a programming language and not a framework. It is an environment. The language is JavaScript; Node.js is the place where that JavaScript runs, along with a standard library of modules like fs (files), http (networking), and path (file paths).
// This is plain JavaScript, running outside a browser.
// There is no document, no window - but there IS a console.
console.log("Hello from Node.js!");
const languages = ["JavaScript", "on", "the", "server"];
console.log(languages.join(" "));Why Node.js exists
In the mid-2000s, most web servers handled each connection with a dedicated thread. Threads are expensive: each one reserves memory, and switching between thousands of them wastes CPU time. Servers often sat idle, waiting for slow things — a database reply, a file read, a network response — while holding a whole thread hostage.
Ryan Dahl created Node.js around a different idea: one thread, never waiting. Instead of blocking while slow work happens, Node.js registers a callback (a function to run later) and immediately moves on to other work. When the slow operation finishes, the callback runs. This style is called non-blocking I/O, and it lets a single Node.js process juggle thousands of simultaneous connections.
- Traditional model: one thread per connection, threads spend most of their time waiting
- Node.js model: one main thread, work is scheduled and callbacks fire when results are ready
console.log("1. Ask for slow work (a 100ms timer)");
setTimeout(() => {
console.log("3. Slow work finished - callback runs");
}, 100);
// Node does NOT wait for the timer. It keeps going:
console.log("2. Meanwhile, the program continues immediately");Where Node.js shines
Because it excels at handling many concurrent connections that spend most of their time waiting on I/O, Node.js is a natural fit for:
- APIs and web servers — REST and GraphQL backends serving JSON
- Real-time apps — chat, live dashboards, multiplayer games (WebSockets)
- Command-line tools — bundlers, linters, and generators (npm itself is a Node program)
- Streaming and proxies — moving data between systems without buffering everything in memory
Node.js is a weaker fit for CPU-heavy work like video encoding or scientific number crunching, because long computations block its single main thread. (There are escape hatches — worker threads — which you'll meet in the performance lesson.)
// Simulate 3 "requests" that each wait a different amount of time.
// One single-threaded Node process handles all of them concurrently.
function handleRequest(name, ms) {
setTimeout(() => {
console.log("Finished: " + name + " (" + ms + "ms)");
}, ms);
}
handleRequest("request A", 300);
handleRequest("request B", 100);
handleRequest("request C", 200);
console.log("All three requests accepted instantly!");Node.js vs JavaScript in the browser
The language is the same, but the environments differ. In the browser, JavaScript gets window, document, and the DOM. In Node.js, those do not exist — instead you get server-side powers.
- Browser:
window,document, DOM APIs,fetch, localStorage; sandboxed away from your files - Node.js:
process,require,Buffer, full file system access, raw network sockets - Both: the core language (variables, functions, classes, promises),
console, timers likesetTimeout
The global object also differs: browsers call it window, Node calls it global, and the standard, works-everywhere name is globalThis.
// In Node.js there is no document or window:
console.log("typeof document:", typeof document); // "undefined"
console.log("typeof window:", typeof window); // "undefined"
// But Node gives you a process object:
console.log("typeof process:", typeof process); // "object"
console.log("Current directory:", process.cwd());What you'll learn in this course
Over 20 levels you will go from zero to building a real REST API. The journey:
- Foundations — installing Node, running programs, modules, npm, and how the runtime works
- Core modules — files, paths, OS info, events, streams, and buffers
- Servers & APIs — the raw
httpmodule, Express.js, and configuration - Advanced — error handling, async patterns, performance, testing, and a final project
Every lesson has runnable code examples and a quiz. Run the examples, change them, break them — that is how the concepts stick.
Check your understanding
5 questionsQuestion 1 of 5
Score: 0