Loading video player…

Follow a Request Through the Chain

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. In app.ts, that’s addRequestId, 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 three app.use() lines have no path, so they take every request. app.use("/problems", problemsRouter) only takes /problems and the paths below it, such as /problems/1, but not /problemsExtra. The router is a list of its own, which gets the path with /problems taken off, and its routes check the method as well as the path.
  • Each function either answers or calls next() to pass the request on. For GET /problems, addRequestId() gave the request an ID, timeRequest() noted the start time, and express.json() found no JSON body to read. Each passed it on. Inside the problems router, the GET / route matched, and its handler answered 200 OK with the problem list.
  • The first answer ends the walk. The log router and the 404 page never saw GET /problems. For GET /log, the three middleware functions ran, the whole problems row was skipped because the path isn’t under /problems, and the log router’s GET / 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 with readJsonBody(), 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 -d and no Content-Type header, which curl labels as form data, got 415 Unsupported Media Type from requireJson(). It never called next(), 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 passed requireJson(), and the handler answered 422.
  • A request that no route in a router matches carries on down the list. GET /problems/2/climbs is a path no route covers. It entered the problems router as GET /2/climbs, where GET /:id doesn’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 415 still got a timing line in the server’s terminal, because timeRequest() ran earlier in the list, before requireJson() 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:

Language: TypeScript Filename: 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:

Language: TypeScript Filename: 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():

Language: Shell
$ 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:

Language: Shell
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.

00:00 In the last lesson, you gave every request to the wall’s API an ID and a timing line. Now you’ll follow requests through them, to see which functions run and which one answers.

00:12 Express tries your routes in order. The first route whose method and path match gets the request, and when none matches, Express sends its 404 page. But your app has middleware now, too.

00:24 Soon you’ll add a check for Ines’s new problems. To put it in the right place, you need to know where these lines sit in that picture. Express keeps one list for your app.

00:36 Each app.use() line goes into it, in the order your code registers them. The first three have no path, so they take every request. The next two have a path, so they only take requests for that path or a path below it.

00:51 Each of those holds a router, and a router is a list of its own, one for the problem routes and one for the climbing log. Below everything sits Express’s 404 page.

01:04 Send a request for the problem list, and follow it from the top. First, addRequestId() gives it an ID and calls next() to pass it on.

01:14 timeRequest() notes the start time and passes it on. There’s no JSON body to read, so express.json() passes it on, too. The path sits under the problems prefix, so the request enters the problems router, with only a slash left.

01:30 The first route there matches GET and slash. Its handler sends the list, and curl gets 200 OK.

01:37 That answer ends the walk. The log router and the 404 page never see this request. Once the response is sent, its timing line appears in the server’s terminal.

01:50 Next, ask for the climbing log. Your turn: pause the video and predict which functions run for this request, from the top.

02:03 The three middleware functions run first, as they do for every request.

02:08 Then the whole problems row is skipped, because the path isn’t under the problems prefix. The log row matches, and its GET route answers with Mei’s, Tunde’s and Sofia’s climbs.

02:22 A route can list more than one function, too. Here, the POST route for new problems lists requireJson() before its handler. It checks the Content-Type header with req.is(), answers 415 when it isn’t JSON, and passes the request on when it is.

02:38 It’s a toy copy of the check the handler already makes, with its own message so you can tell who answered. You don’t need to type it.

02:48 Now send a new problem with -d and no Content-Type header, so curl labels the body as form data. The three middleware functions pass it on. express.json() only reads bodies labeled as JSON, so it leaves this one alone.

03:03 In the problems router, the two GET routes have the wrong method. The POST route matches, and its first function runs. The body isn’t JSON, so requireJson() answers 415 itself, with its own message.

03:18 It never calls next(), so the handler beside it never runs. The server’s terminal still shows a timing line for the 415, because the timer ran earlier.

03:29 Send the last lesson’s JSON body with no name, and requireJson() passes it on. The handler answers 422.

03:38 Last, try a path no route covers. It enters the problems router, matches no route there, and walks on to Express’s 404. So Express walks one list from the top.

03:51 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.

04:07 That’s why the order of your lines decides what each request meets. So what happens when a line sits in the wrong place? You’ll find out in the next lesson, Register Middleware in Order.

Become a Member to join the conversation.