Loading video player…

Follow a Request Through Express

In this lesson, you gave the café’s app a second route and followed three requests through it, to see what Express does with a request between arriving and reaching your handler.

Key Points

  • Each app.get() call adds a route to a list that the app keeps. A route holds a method, a path and a handler, and the routes go in the order your code adds them. In app.ts, the root path / is first and /tickets is second.
  • Node calls the app once for every request. The app is also a function. When you call app.listen(), Express hands it to Node as the request listener, and the app takes each request down its list, one route at a time.
  • The first route whose method and path both match gets the request. Express calls that route’s handler and stops looking, like the if and else in server.ts, where the first test that passes wins and nothing below it is checked. GET / matched the first route, so the ticket route never saw it. GET /tickets skipped the first route, whose path didn’t match, and matched the second.
  • A handler answers by calling a response method such as res.json(), not by returning. Express ignores a handler’s return value. A handler that returned the tickets instead would leave curl waiting with no response at all. Writing return res.json(...) is fine, though, because the call is what answers.
  • When no route matches, Express answers by itself. POST /tickets matched the second route’s path but not its method, so Express skipped it too. Express then sent a 404 Not Found page that says Cannot POST /tickets, doing the job of the starting server’s else branch. A 405 Method Not Allowed is the more precise answer for a path that exists, but Express’s fallback only knows that no route matched. However, the 404 page is HTML, not the API’s error shape, so error handling later on the learning path replaces it.

Express tries your routes in order, and the first one whose method and path both match gets the request. Its handler answers once, with a call such as res.json(). Express now finds the route, and your handler still chooses the answer.

The Final Code

Here’s app.ts with its new route for the root path:

Language: TypeScript Filename: app.ts
import express from "express";
import { getTickets } from "./tickets.ts";

const PORT = 3000;

const app = express();

app.get("/", (req, res) => {
  res.json({ cafe: "Pembury Library repair café", tickets: "/tickets" });
});

app.get("/tickets", (req, res) => {
  res.json(getTickets());
});

app.listen(PORT, (error) => {
  if (error) {
    throw error;
  }
  console.log(`Listening on http://localhost:${PORT}`);
});

With the app running under node --watch app.ts, the root path answers with the café’s JSON:

Language: Shell
$ curl -i http://localhost:3000/
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 60
ETag: W/"3c-kMZYPAh1jsfyF8L7KrsNw5eXv6c"
Date: Fri, 27 Nov 2026 01:38:03 GMT
Connection: keep-alive
Keep-Alive: timeout=5

{"cafe":"Pembury Library repair café","tickets":"/tickets"}

A new ticket sent with POST finds no route yet:

Language: Shell
$ curl -i -X POST http://localhost:3000/tickets
HTTP/1.1 404 Not Found
X-Powered-By: Express
Content-Security-Policy: default-src 'none'
X-Content-Type-Options: nosniff
Content-Type: text/html; charset=utf-8
Content-Length: 147
Date: Fri, 27 Nov 2026 01:38:03 GMT
Connection: keep-alive
Keep-Alive: timeout=5

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /tickets</pre>
</body>
</html>

Dev’s desk still can’t add a ticket. In the next lesson, Read a JSON Body With express.json(), you’ll build the route that takes one.

00:00 In the last lesson, Express answered the ticket list with a one-line handler. So what happens to a request between arriving at your app and reaching that handler?

00:09 To find out, you’ll follow three requests through it.

00:14 First, give the app a second route, for the root path. Anyone who opens the API’s own address, like Dev’s laptop at the desk, gets a little JSON that names the café and says where the tickets live.

00:28 Save, and Node’s watch mode restarts the app. Ask for the root path with curl, and the café’s JSON comes back with 200 OK.

00:38 Each call to app.get() adds a route to a list that the app keeps. A route holds a method, a path and a handler. The routes go in the order your code adds them, so the root path is first and the ticket list is second.

00:54 The app is also a function. When you call app.listen(), Express hands it to Node as the request listener. So Node calls the app once for every request that arrives, and the app takes that request down its list, one route at a time.

01:11 Here’s the request you sent for the root path. The first route has the method GET and the path slash. Both match, so Express calls that route’s handler. It works like the if and else in your starting server, where the first test that passes wins and nothing below it is checked.

01:29 Next, ask for the tickets. The first route’s method matches, but its path doesn’t, so Express moves on. The second route matches both, and its handler runs.

01:41 It calls res.json(), and the response travels back to curl, with 200 OK and all five tickets. Watch out here: the handler answers by calling res.json(), not by returning.

01:55 If it returned the tickets instead, Express would ignore that value, and curl would wait with no response at all. Writing return res.json() is fine, though, because the call is what answers.

02:09 Soon, Dev’s desk will add a new ticket by sending a POST request to the ticket path. Your app has no route for that yet, but curl’s -X option can send one now.

02:20 The second route has the right path, though. Your turn: pause the video and predict which status code comes back.

02:32 It’s a 404 Not Found. Neither route matches both. The second has the path but not the method, so Express skips it. When the list runs out, Express answers with a small HTML page that names the method and path it couldn’t match.

02:47 If you guessed 405 Method Not Allowed, that’s the more precise answer for a path that exists, but Express’s fallback only knows that no route matched.

02:59 That’s the job the else branch did in the starting server, done for you. However, it’s an HTML page, not the API’s error shape, so a later lesson replaces it.

03:12 Express tries your routes in order, and the first one whose method and path both match gets the request. Its handler answers once, with a call such as res.json().

03:24 Finding the route is now Express’s job. Choosing the answer is still yours. And Dev’s desk still can’t add a ticket. In the next lesson, Read a JSON Body With express.json(), you’ll build the route that takes one.

Become a Member to join the conversation.