Read a Plot Number From the Path

In the last lesson, your plot board learned to check the method and the path together using a table of routes. Every route in that table matches one fixed path, though, and one of Mrs. Okafor’s most common calls doesn’t fit: How big is plot 7? Ask your board now, and it doesn’t know:

Language: Shell
$ curl -i http://localhost:3000/plots/7
HTTP/1.1 404 Not Found
Content-Type: application/json; charset=utf-8
Date: Thu, 26 Nov 2026 16:57:11 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Content-Length: 78

{"error":{"code":"not_found","message":"Nothing at /plots/7.","details":null}}

In this lesson, you’ll make one route answer for every plot, read the plot number out of the path, and decide what the board sends for addresses that come close but don’t match. Start by looking at what the path is made of.

Split the Path Into Segments

Your table can’t sensibly list every plot. Twenty-four rows would repeat one handler, and you’d need another row whenever the society adds a plot. Instead, the router needs to see that /plots/7 has the shape of a plot’s address, which is plots followed by one more piece.

The pathname is a string, and the .split() method cuts it wherever a separator appears. A short session in Node.js’s REPL shows what you get when you cut a pathname at each slash:

Language: JavaScript
> "/plots/7".split("/")
[ '', 'plots', '7' ]
> "/plots".split("/")
[ '', 'plots' ]

Each piece between slashes is a path segment. The first is an empty string, because nothing comes before the leading slash. So a plot’s address splits into three segments, with "plots" second and the plot number third. The number arrives as the string "7", like everything else in a URL.

Above the listener in server.ts, add a function that turns a plot’s pathname into one route path:

Language: TypeScript Filename: server.ts
function routePath(pathname: string): string {
  const segments = pathname.split("/");
  if (
    segments.length === 3 &&
    segments[1] === "plots" &&
    segments[2] !== ""
  ) {
    return "/plots/:number";
  }
  return pathname;
}

For /plots/7, /plots/99, or any other plot address, routePath() returns "/plots/:number". The last condition keeps out /plots/, whose third segment is empty. Every other pathname comes back unchanged, so /plots and /waitlist still match their own rows. The :number part is only a label for a segment that holds a value, following a common convention for path parameters.

In the listener, call routePath() right after creating the URL object, and filter on its result instead of on url.pathname:

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.