Your store now saves every ticket safely, but app.ts has grown to about 150 lines: the store, two limits, five helper functions, five ticket routes, and the call that starts the server. That’s one file for one resource. Dev would also like the desk to know which volunteers are in the hall today. A second resource in the same file means scrolling past every ticket route to find a volunteer route.
In this lesson, you’ll move the ticket routes into a module of their own, give each path one place that lists its methods, and start the server from a separate file. Every endpoint will still answer at the same URL, which is how you’ll know the move worked. Start by giving the tickets a file of their own.
Move the Ticket Routes Into a Router
The ticket routes need a home outside app.ts, and Express has an object for that. A router is a mini app: it has its own .get(), .post(), .patch(), and .delete() methods, but no .listen(), so on its own it answers nothing. You create one with express.Router(), export it from its module, and the app hands it requests.
Make a folder named routes in the project, and create routes/tickets.ts in it with these lines at the top:
routes/tickets.ts
import express from "express";
import type { Request, Response } from "express";
import { createTicketStore } from "../store.ts";
import {
findTickets,
getPage,
SORT_FIELDS,
sortTickets,
toNewTicket,
toStatus,
} from "../tickets.ts";
const store = await createTicketStore("tickets.json");
export const ticketsRouter = express.Router();
The imports are the ones from app.ts, with ../ in front of each local import path, because this module sits one folder down. The store line moves too, since only the ticket routes use the store. Imports and exports work as you know them from Modules and JSON, so ticketsRouter is a const any module can import.
Now cut the rest of the ticket code out of app.ts and paste it below the router: the DEFAULT_LIMIT and MAX_LIMIT constants, the five helpers from errorBody() to sendBadQuery(), and the five routes. In each route’s first line, change app to ticketsRouter, so app.get("/tickets/:id", ...) becomes ticketsRouter.get("/tickets/:id", ...). What’s left of app.ts builds the app and hands the router its requests:
app.ts
import express from "express";
import { ticketsRouter } from "./routes/tickets.ts";
const PORT = 3000;
const app = express();
app.use(express.json());
app.use("/tickets", ticketsRouter);
app.listen(PORT, (error) => {
if (error) {
throw error;
}
console.log(`Listening on http://localhost:${PORT}`);
});
The line app.use("/tickets", ticketsRouter); mounts the router at /tickets, its mount path. Every request for /tickets or a path beneath /tickets/ goes to the router. For example, /tickets/1 matches, but /tickets2 doesn’t. The router tries its routes in order, as the app did. The express.json() line stays on the app, so bodies are already parsed when a request reaches the router.
Run npm run check, which passes. This lesson’s requests expect the café’s five starting tickets. Stop the app if it’s running, and save a copy of your tickets.json if you want to keep it. Then copy tickets.json from the course’s starting project, and start the app with node --watch app.ts. Before you send anything, predict what this request gets back:
$ curl -i http://localhost:3000/tickets/1
$ curl -i http://localhost:3000/tickets/1
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: 148
Date: Fri, 27 Nov 2026 03:52:44 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 GET /tickets/1</pre>
</body>
</html>
You get Express’s own HTML 404 page, with no ticket. GET /tickets gets a 404 with Cannot GET /tickets instead, so every endpoint is gone. The routes are all still there, though, one level deeper than you’d expect. A request for /tickets/tickets/1 sends back Priya’s toaster.
To see why, look at what the router receives. Add these lines at the start of the GET /tickets/:id route in routes/tickets.ts:
routes/tickets.ts
console.log({
originalUrl: req.originalUrl,
baseUrl: req.baseUrl,
url: req.url,
});