Node.js Tutorial with Examples.
Node.js backend tutorial: the http module, npm and node --watch, Express and Fastify APIs, Zod validation, Pino logging, JWT auth and graceful shutdown.
Beginner
Start here — no prior experience needed
What Node.js Is
See what is really inside Node.js, V8 and libuv, then watch a browser-only global fail, a sync read finish first, and a busy loop delay a 0 ms timer.
The HTTP Module and Why Frameworks Exist
Build a server with Node's built-in http module, route by hand with regex, and parse a POST body yourself, to see exactly what Express replaces.
package.json, npm Scripts, and node --watch
Run npm scripts, install a dependency and read the caret it adds to package.json, then watch node --watch detect a real file change and restart.
Express Routing, Request, and Response
Install Express and rewrite the manual http router as named routes, compare its default 404 page to a custom one, and read req.params and req.query.
Middleware and the Request Pipeline
Write logging and error-handling middleware in Express, then watch registering the error handler before the routes silently break it.
Validating Requests with Zod
Write a Zod schema for a POST body, turn failures into 400 responses with per-field errors, and use the parsed, trimmed output instead of the raw body.
Intermediate
For developers with core concepts down
Structured Logging with Pino
Replace console.log with Pino, attach a request id to every log line through pino-http's child logger, and read the one-line JSON it prints per request.
Async Route Handlers and Errors in Express 5
Watch an Express 4 async handler crash the whole process on an unhandled rejection, then see Express 5 forward the same throw to the error handler with no wrapper.
Testing an HTTP API with node:test and fetch
Test a real running Express server with Node's built-in test runner and fetch, no supertest dependency, including what a real assertion failure prints.
The Same API in Fastify
Rebuild the users API in Fastify with JSON Schema route validation, a hook, and automatic response serialization that strips fields the schema doesn't list.
JWT Auth Middleware
Issue a signed JWT on login, verify it in Express middleware, and see the exact errors for a missing token, a malformed one, and a genuinely expired one.
Graceful Shutdown and Health Checks
Watch Docker SIGKILL a server mid-request with no handler, then add one that fails readiness, drains the same slow request, and exits cleanly instead.
File Uploads with multer
An unhandled MulterError crashes into a 500; catch its codes for 413 and 400, see busboy strip a traversal filename, and fool a fileFilter with a lie.
A WebSocket Server with ws
Attach ws to a node:http server, broadcast to every client, reject a bad upgrade token with a 401, and terminate an unresponsive client with ping/pong.
OpenAPI Docs from Fastify Schemas
Generate OpenAPI docs from Fastify route schemas with @fastify/swagger, see a schema-less route's empty entry, and watch a response schema strip a field.
PostgreSQL with pg: Pools and Transactions
Run PostgreSQL in Docker, connect with pg's Pool, use parameterised queries, commit or roll back a transaction, and exhaust the pool with a leaked client.
Advanced
Production-grade patterns for experienced engineers
Don't Block the Event Loop
Watch a CPU-bound hash route stall /healthz for seconds, then move the work to worker_threads so health checks stay fast while the same job keeps running.
NestJS: Modules, Controllers, and Providers
Build a NestJS module, controller, and service, fix a null response that should 404, then see a missing compiler flag turn dependency injection into a 500.
NestJS Validation with Zod and Standard Schema
Reuse a Zod schema as a NestJS pipe via the Standard Schema spec, watch an unwired schema validate nothing, then reshape the error response safely.
NestJS Guards, Interceptors, and Exception Filters
Log a guard, pipe, interceptor, and filter running around one NestJS handler, then build a 403 guard, a timing interceptor, and an error-reshaping filter.
Also part of Node.js
Taught in other topics. Each link opens the lesson in its home topic.
Files, Streams, and the Command Line in Node.js
Learn Node.js CLI args, env vars, fs/promises, readline, and streams, including backpressure and error propagation through stream/promises pipeline.
JavaScript Event Loop Explained
Understand how JavaScript's single-threaded event loop works, why microtasks run before macrotasks, and how to reason about async execution order.
JavaScript Modules: ESM and CommonJS
Understand ES modules and CommonJS, their syntax differences, dynamic imports for code splitting, and how bundlers like Vite and webpack fit in.
Introduction to Docker
Run your first Docker containers and see what they are: ordinary Linux processes sharing the host kernel, started from an image in well under a second.
Managing Docker Containers
Run, inspect, stop and remove containers: the lifecycle, port publishing, exec, logs, docker stop versus SIGKILL, memory limits and docker cp.
Multi-Stage Builds
Use multi-stage Dockerfiles to produce small, secure production images by separating build and runtime environments.
Docker Compose
Define and run multi-container applications with Docker Compose. Services, networks, volumes, and environment configuration.
Node.js with TypeScript
Build a typed Express 5 API: tsconfig for Node, validated env config, typed routes and middleware, async errors and Zod validation, with real curl output.
Unsafe File Uploads: secure_filename, Allowlists and Size Limits
A Flask avatar upload writes outside its folder and accepts any file. Fix it with secure_filename, an extension allowlist and MAX_CONTENT_LENGTH.
Decorators in TypeScript
Standard TC39 decorators for methods, classes, accessors and fields, then legacy experimentalDecorators with metadata-driven DI — all with real output.