In this lesson, you followed requests to the wall’s API through its middleware and routers, to see which functions run for each one and which one answers.
Key Points
- Express keeps one list for your app. Each
app.use()line goes into it in the order your code registers it. Inapp.ts, that’saddRequestId,timeRequest,express.json(), the problems router and the log router, with Express’s own 404 page below everything. - An
app.use()line matches by path, a route by method and path. The first threeapp.use()lines have no path, so they take every request.app.use("/problems", problemsRouter)only takes/problemsand the paths below it, such as/problems/1, but not/problemsExtra. The router is a list of its own, which gets the path with/problemstaken off, and its routes check the method as well as the path. - Each function either answers or calls
next()to pass the request on. ForGET /problems,addRequestId()gave the request an ID,timeRequest()noted the start time, andexpress.json()found no JSON body to read. Each passed it on. Inside the problems router, theGET /route matched, and its handler answered200 OKwith the problem list. - The first answer ends the walk. The log router and the 404 page never saw
GET /problems. ForGET /log, the three middleware functions ran, the whole problems row was skipped because the path isn’t under/problems, and the log router’sGET /route answered with Mei’s, Tunde’s and Sofia’s climbs. - A route can list more than one function. In
problemsRouter.post("/", requireJson, async (req, res) => { ... }),requireJson()runs before the handler. It’s a toy copy of the Content-Type check the handler already makes withreadJsonBody(), one step earlier and with its own message. - The first function in a route’s list can answer for it. A new problem sent with
-dand no Content-Type header, which curl labels as form data, got415 Unsupported Media TypefromrequireJson(). It never callednext(), so the handler never ran.express.json()had left that body alone, because it only reads bodies labeled as JSON. The JSON body with no name passedrequireJson(), and the handler answered422. - A request that no route in a router matches carries on down the list.
GET /problems/2/climbsis a path no route covers. It entered the problems router asGET /2/climbs, whereGET /:iddoesn’t match the extra segment, so it matched none of the routes and walked on to Express’s 404,Cannot GET /problems/2/climbs. - Functions earlier in the list still did their work. The
415still got a timing line in the server’s terminal, becausetimeRequest()ran earlier in the list, beforerequireJson()answered.
Express walks one list from the top. An entry runs only when it matches: an app.use() line by its path alone, a route by its method and path. Each function either answers or calls next() to pass the request on, and the first answer ends the walk.
The Final Code
This lesson didn’t change app.ts. Here it is as the list Express walks, top to bottom:
app.ts
import express from "express";
import { addRequestId, timeRequest } from "./middleware.ts";
import { problemsRouter } from "./routes/problems.ts";
import { logRouter } from "./routes/log.ts";
export const app = express();
app.use(addRequestId);
app.use(timeRequest);
app.use(express.json());
app.use("/problems", problemsRouter);
app.use("/log", logRouter);
The video’s requireJson() was only there to show a route with two functions. It isn’t part of the wall’s API, so you don’t need to add it. In the video, it sat in routes/problems.ts, listed in the POST route before the handler:
routes/problems.ts
function requireJson(
req: Request,
res: Response,
next: NextFunction,
) {
if (req.is("application/json") === false) {
const message = "requireJson: send a JSON body.";
res.status(415).json(errorBody("not_json", message));
return;
}
next();
}
problemsRouter.post("/", requireJson, async (req, res) => {
const body = readJsonBody(req, res);
// ...
});
With it in place, a new problem sent as form data stopped at requireJson():
$ curl -i -d "grade=V3" http://localhost:3000/problems
HTTP/1.1 415 Unsupported Media Type
X-Powered-By: Express
X-Request-Id: 19261e37-e916-432e-ba83-4b1f1056bb2f
Content-Type: application/json; charset=utf-8
Content-Length: 87
ETag: W/"57-M2UWk9g2vccPSkRz234jwo9OZUQ"
Date: Fri, 27 Nov 2026 11:06:13 GMT
Connection: keep-alive
Keep-Alive: timeout=5
{"error":{"code":"not_json",
"message":"requireJson: send a JSON body.","details":null}}
The body is wrapped here for display. Its not_json code and message come from requireJson(), not from the handler’s readJsonBody(). The server’s terminal logged a line for each of the video’s five requests, the 415 included:
b6c063ff-0e0d-46e0-975b-a4bb1131b6bf GET /problems 200 5.3 ms
55ec9be0-5795-4813-a8f1-9a4cd6f46f1c GET /log 200 0.9 ms
19261e37-e916-432e-ba83-4b1f1056bb2f POST /problems 415 1.4 ms
183cd1e0-9693-482b-9d9e-f60a7a4fade4 POST /problems 422 6.8 ms
94ca0423-864c-461f-b31d-a74997df51ac GET /problems/2/climbs 404 1.8 ms
Your IDs and times will differ. What happens when one of these lines sits in the wrong place? You’ll find out in the next lesson, Register Middleware in Order.