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. Inapp.ts, the root path/is first and/ticketsis 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
ifandelseinserver.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 /ticketsskipped 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 leavecurlwaiting with no response at all. Writingreturn res.json(...)is fine, though, because the call is what answers. - When no route matches, Express answers by itself.
POST /ticketsmatched the second route’s path but not its method, so Express skipped it too. Express then sent a404 Not Foundpage that saysCannot POST /tickets, doing the job of the starting server’selsebranch. A405 Method Not Allowedis 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:
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:
$ 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:
$ 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.