Basics
Everything an Express app is made of, on one growing cats API: routes and the handlers that answer through res, request bodies, middleware that runs around them, routers that split an app into parts, the request and response in detail, and errors. Express 5, the version current since 2024.
- lessons
- 6
- level
- beginner
- time
- 3 hours
Routes and handlers
An app is routes, each a method, a path and a handler that answers through res.
- 1First steps
An Express app is a list of routes and a call to listen(). Answer the cats as JSON, one cat by its id, a 404 for a missing one, and a name as plain text rather than the HTML Express assumes for a string.
Read the theory - 2Routes and bodies
Read request bodies with express.json(), create, change and delete cats, and declare the routes one path shares once with app.route(). Plus what Express 5 leaves in req.body when there is nothing to parse.
Read the theory
Structure
What runs around a route, and how an app is split into parts.
- 3Middleware
Express is a chain of functions, and a route is only the last link. Write middleware that numbers every request, logs each one with its final status, and guards two routes with an API key, and see why the order they are mounted in decides what happens.
Read the theory - 4Routers
Split the app into routers, one per resource and one file each, mounted under their paths. Load a cat once for every route with :id, nest an owner's cats under the owner, and see what the request knows about where it is.
Read the theory
Requests, responses and errors
What a request and a response carry in detail, and what happens when something goes wrong.
- 5Request and response
The request and the response in detail: answer JSON or CSV with res.format(), refuse a body that is not JSON with req.is(), remember a preference in a cookie with cookie-parser, and redirect to a resource that can move.
Read the theory - 6Errors
Let routes throw and answer every error in one place: an error class that carries its status, a 404 for anything no route matched, body-parser's error for broken JSON, Express 5 catching async handlers, and a 500 that tells the client nothing about your internals.
Read the theory