Introduction to Node.js
Node.js lets you run JavaScript outside the browser. Built on Chrome’s V8 engine and libuv, it is designed for I/O-heavy, concurrent workloads — APIs, real-time chat, streaming, and CLI tools.
What is Node.js?
Node.js is not a framework; it is a JavaScript runtime that provides:
- A JavaScript engine (V8) for executing code
- Core modules for file system, HTTP, crypto, and streams
- npm — the world’s largest open-source package registry
- An event loop that handles thousands of concurrent connections on a single thread
Because JavaScript is the language of the web, Node.js enables full-stack development with one language on both client and server.
Event-Driven, Non-Blocking I/O
Traditional server models spawn a thread per request. Node.js uses a single thread and an event loop — when I/O (database, file, network) is waiting, Node.js handles other work instead of blocking.
const fs = require('fs');
console.log('Start');
fs.readFile('/etc/hosts', 'utf8', (err, data) => {
console.log('File read complete');
});
console.log('End');
// Output order:
// Start
// End
// File read complete
This model excels for many concurrent, short-lived connections (REST APIs, WebSockets). CPU-bound tasks (video encoding, heavy math) should run in worker threads or separate services.
Key Features
| Feature | Benefit |
|---|---|
| npm ecosystem | 2M+ packages for every need |
| Cross-platform | Windows, macOS, Linux, containers |
| JSON native | Natural fit for REST APIs |
| Streams | Efficient processing of large files |
| ES modules | Modern import/export syntax supported |
Node.js vs Other Runtimes
Node.js vs Python (Flask/Django): Node.js handles concurrent I/O efficiently with its event loop. Python suits CPU/data workloads and has stronger ML libraries. Many teams use Node.js for APIs and Python for data pipelines.
Node.js vs Java (Spring Boot): Java offers stronger static typing and mature enterprise tooling. Node.js starts faster, has a smaller memory footprint per process, and shares code with front-end teams.
Node.js vs Deno/Bun: Deno and Bun are newer runtimes with TypeScript-first design and built-in tooling. Node.js remains the default for production due to ecosystem size and hosting support.
Architecture Overview
┌─────────────────────────────────────┐
│ Your Application │
│ (Express, Fastify, NestJS, …) │
├─────────────────────────────────────┤
│ Node.js Core Modules │
│ http fs crypto stream path │
├─────────────────────────────────────┤
│ V8 Engine │ libuv (I/O) │
└─────────────────────────────────────┘
Your First HTTP Server
// server.js
const http = require('http');
const server = http.createServer((req, res) => {
if (req.url === '/health') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ status: 'ok' }));
return;
}
res.writeHead(404);
res.end('Not Found');
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000');
});
Run it:
node server.js
curl http://localhost:3000/health
# {"status":"ok"}
Modern ES Module Syntax
// app.mjs
import http from 'node:http';
const PORT = process.env.PORT ?? 3000;
http.createServer((req, res) => {
res.end('Hello from ES modules');
}).listen(PORT);
Enable ES modules in package.json:
{
"type": "module"
}
Common Use Cases
- REST and GraphQL APIs — Express, Fastify, Apollo Server
- Real-time apps — Socket.io, WebSockets for chat and live dashboards
- Serverless functions — AWS Lambda, Vercel, Cloudflare Workers
- CLI tools — npm packages like
eslint,prettier,create-react-app - Microservices — lightweight services behind an API gateway
- BFF (Backend for Frontend) — tailor APIs to web and mobile clients
The npm Ecosystem
Every Node.js project starts with package.json:
mkdir my-api && cd my-api
npm init -y
npm install express
Scripts in package.json automate development:
{
"scripts": {
"start": "node src/index.js",
"dev": "node --watch src/index.js",
"test": "node --test"
}
}
Prerequisites
This track assumes you have completed the JavaScript basics section. You should understand variables, functions, objects, arrays, and promises before diving into Node.js.
What Comes Next
Follow this track in order: Setup and First App, modules, npm, the file system and HTTP APIs, Express, databases, security, deployment, and testing. By the end you will build and ship production Node.js services.