Read the Ticket ID From the Path

Three rows of your endpoint table start with /tickets/:id and describe routes that look up a ticket, change its status, and delete it. Dev’s desk needs all three on a busy Saturday, but your app only answers /tickets so far. Ask it for /tickets/1, and Express still sends its HTML 404 page.

As a client, you’ve put IDs in URLs many times. Now your server has to read one back out of the path. In this lesson, you’ll build the three /tickets/:id routes from the table, answer an unknown ID with a 404, and make POST /tickets say where its new ticket lives. Reading one ticket is the simplest of the three, so start there.

Read the ID From the Path

Looking up a ticket is data work with no HTTP in it, so it goes in tickets.ts, next to getTickets(). Add this function at the bottom of the file:

Language: TypeScript Filename: tickets.ts
export function getTicket(id: string): Ticket | undefined {
  return tickets.find((ticket) => ticket.id === id);
}

The .find() method returns the first ticket whose id matches, or undefined when none does. So getTicket() leaves the decision about a missing ticket to its caller.

In app.ts, import getTicket next to the other functions. A missing ticket needs the same answer in all three /tickets/:id routes, so add a helper below errorBody() that sends it in the API’s error shape:

Language: TypeScript Filename: app.ts
function sendNotFound(res: Response, id: string) {
  const message = `No ticket has the id ${id}.`;
  res.status(404).json(errorBody("ticket_not_found", message));
}

The Response type comes from @types/express. Import it below the line that imports express, with import type because it’s only a type:

Language: TypeScript Filename: app.ts
import type { Request, Response } from "express";

You’ll use Request later in this lesson. Now add the route below GET /tickets, with the path written exactly as the table writes it:

Language: TypeScript Filename: app.ts
app.get("/tickets/:id", (req, res) => {
  const ticket = getTicket(req.params.id);
  if (ticket === undefined) {
    sendNotFound(res, req.params.id);
    return;
  }
  res.json(ticket);
});

The :id in the path is a route parameter. Express matches any single path segment in its place, so /tickets/1 and /tickets/42 both reach this handler. It puts whatever the segment holds in req.params, under the parameter’s name, so req.params.id is the ID the desk asked for.

Colon parameters are most of what you’ll write. Express 5 also has named wildcards such as /*rest and optional parts in braces such as /tickets{/:id}. It throws an error at startup for the regular expression characters, such as ?, that older Express code put in paths.

The ID in /tickets/1 looks like a number, so what type does req.params.id have? Add this line at the start of the handler to find out:

Language: TypeScript Filename: app.ts
console.log(req.params.id, typeof req.params.id);

Save, and ask for Priya’s ticket from your second terminal. Her ticket comes back with a 200, and its JSON is wrapped here for display:

Language: Shell
$ curl -i http://localhost:3000/tickets/1
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 118
ETag: W/"76-kgJG6Emv22CgnEzBl6l1OHqRGGc"
Date: Fri, 27 Nov 2026 02:44:00 GMT
Connection: keep-alive
Keep-Alive: timeout=5

{"id":"1","name":"Priya","item":"toaster","category":"electrical",
"fault":"Only one side heats up","status":"waiting"}

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.