The built-in http module, why Express exists, routing, middleware pipelines, route parameters, query strings, and sending JSON responses.
Module F-6 — Building HTTP Servers and REST APIs with Express
What this module covers: Express is the most widely used Node.js web framework — and for good reason. It takes the built-in
httpmodule and adds routing, middleware, and a clean request/response API without imposing a rigid structure. This module covers everything you need to build a real REST API: routing, route parameters, query strings, request bodies, middleware pipelines, and error responses. By the end you will have a working multi-route API with proper HTTP semantics.
From Raw http to Express
You saw the built-in http module in F-1:
This works, but it has no routing. Every request — regardless of URL or method — hits the same handler. To build a real API you need to route GET /users to one handler, POST /users to another, DELETE /users/:id to a third. You also need to parse JSON bodies, read headers, set response codes properly, and handle errors consistently.
You could implement all of that yourself on top of http. Express already did it — and did it well.
Version note: you're likely installing Express 5.
npm install expresshas pulled Express 5.x since September 2024 — that's thelatesttag now, not Express 4. This module's code works the same under both versions, but two differences are worth flagging up front so the rest of the module doesn't silently assume Express 4 behavior:
- Async error forwarding. In Express 5, if an
asyncroute handler's promise rejects, Express automatically forwards that rejection to your error-handling middleware. You don't strictly need the manualtry { ... } catch (err) { next(err) }pattern shown in the Error Handling section below — though it's still fine practice, and it is required if you're on Express 4 (still extremely common in existing production codebases). The Error Handling section explains why that pattern exists in the first place.- Stricter routing. Express 5 ships a new router (path-to-regexp v8) that rejects some Express 4-era path patterns — most notably bare wildcards like
app.get('*', ...), which now throw at startup. Use a named wildcard likeapp.get('/*splat', ...)instead.Run
npm ls expressif you're ever unsure which version a project is actually running.
Your First Express Server
The Express API:
express()creates an application instanceapp.use(...)adds middleware (runs on every request)app.get(path, handler)registers a GET routereq— the incoming request objectres— the response objectres.json(data)— sends JSON withContent-Type: application/jsonand status 200
HTTP Methods and REST Conventions
REST APIs use HTTP methods to express intent:
| Method | Meaning | Example |
|---|---|---|
GET | Read a resource | GET /users — list all users |
POST | Create a resource | POST /users — create a user |
PUT | Replace a resource entirely | PUT /users/42 — replace user 42 |
PATCH | Update part of a resource | PATCH /users/42 — update user 42's email |
DELETE | Remove a resource | DELETE /users/42 — delete user 42 |
Express has a method for each:
Route Parameters
Route parameters are named placeholders in the URL path, prefixed with :. Express captures them and puts them in req.params.
Important: route parameters are always strings, even if the URL contains a number. Convert them explicitly:
Query Strings
Query strings are the ?key=value part of a URL. Express parses them automatically into req.query.
Query string values are always strings — convert them as needed.
Request Bodies
For POST, PUT, and PATCH requests, data comes in the request body. With express.json() middleware registered, Express automatically parses JSON bodies into req.body.
Sign in to keep reading
The rest of this module is free — sign in with Google to unlock it and track your progress.
Sign in & RegisterDiscussion
0Join the discussion