Express Intro
Express is a small, unopinionated web framework for Node.js. It wraps the built-in node:http server and gives you three things that make server code pleasant to write:
- Routing — map a method and a path (
GET /books/42) to a function. - Middleware — a pipeline of small functions that every request flows through (logging, body parsing, authentication…).
- Helpers —
res.json(),res.status(),req.params,req.queryand friends, so you rarely touch raw headers.
This course uses Express 5 (the current major version) and, in its third part, NestJS 11, a structured framework that runs on top of Express.
Note: If you completed the Node.js course you have already met Express briefly. This course goes much deeper: from the first route to a validated, authenticated and tested REST API.
Why Express?
| Strength | What it means for you |
|---|---|
| Minimal core | You learn a handful of concepts and compose the rest |
| Huge ecosystem | Thousands of middleware packages (CORS, sessions, uploads, logging…) |
| Stable API | Code written years ago mostly still works |
| Foundation for others | NestJS uses Express under the hood by default; many frameworks copy its style |
| Async-friendly (v5) | Rejected promises in handlers reach your error handler automatically |
Express doesn't force a project layout, an ORM or a validation library on you. That freedom is great for small services and learning; for big teams, NestJS adds the structure that Express leaves out.
How a request flows
When a request arrives, Express walks through the functions you registered, in the order you registered them, until one of them sends a response:
request ──► logger ──► express.json() ──► auth check ──► route handler ──► response
│ │ │
└── next() ────┴──── next() ─────┘ (each step passes control on)Everything — routes included — is a middleware-like function. Keep this picture in mind; most Express bugs come from functions running in an unexpected order.
Your first look
Here is a complete Express program. It creates an app, registers one route, starts the server on a random free port (0), calls itself with fetch, prints the reply and shuts down. Every runnable example in this course follows this pattern so that the output is the same on every machine.
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.json({ message: 'Hello from Express 5' });
});
const server = app.listen(0, async () => {
const base = `http://localhost:${server.address().port}`;
const res = await fetch(base + '/');
console.log(res.status, await res.json());
server.close();
});200 { message: 'Hello from Express 5' }
Tip: On this site
nodeexamples are simulated with the saved output. Every one of them was executed with real Node 24 and Express 5.1, so you can copy any example into a file and run it locally afternpm install express.
Express compared to plain Node
The same "hello" endpoint with only node:http needs you to set the status code and headers and serialise the JSON by hand:
const http = require('node:http');
const server = http.createServer((req, res) => {
if (req.method === 'GET' && req.url === '/') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'Hello from node:http' }));
return;
}
res.writeHead(404).end();
});
server.listen(0, async () => {
const base = `http://localhost:${server.address().port}`;
const ok = await fetch(base + '/');
const missing = await fetch(base + '/nope');
console.log(ok.status, await ok.json());
console.log(missing.status);
server.close();
});200 { message: 'Hello from node:http' }
404
For one route this is fine. With fifty routes, path parameters, JSON bodies and error handling, the hand-written if chain becomes hard to maintain — that's the gap Express fills.
What you'll build
| Part | Topics |
|---|---|
| Express Basics | routing, parameters, request/response, middleware, routers, errors, status codes |
| Building APIs | REST design, CRUD, validation, pagination, CORS, auth, rate limiting, config, logging, tests, deployment |
| NestJS | modules, controllers, providers, dependency injection, pipes, guards, interceptors, exception filters |
| Practice | exercises and a bookmarks REST API built step by step |
Express 5 in one paragraph
Express 5 was released as stable in late 2024 after a long beta. The headline changes from v4: async handlers are supported (a rejected promise becomes next(err)), route path syntax became stricter (/*splat instead of /*, {/:id} for optional segments), several deprecated methods were removed (res.send(status, body), req.param(), app.del()), and Node 18 or newer is required. You'll meet each of these where it matters.
Test yourself
What does Express add on top of Node's http module?
- A database layer
- Routing, middleware and request/response helpers
- A template for React apps
- A replacement for the V8 engine
In which order does Express run your middleware and routes?
- Alphabetically by path
- Most specific path first
- In the order they were registered
- In random order
Why do the examples call app.listen(0, …)?
- Port 0 is the default HTTP port
- It asks the OS for any free port, so examples never clash
- It disables networking
- It makes the server single-threaded