Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Follow a URL Into a Router

In this lesson, you followed a URL from the café’s app into the tickets router, and then into a notes router mounted inside it, to see which part of the URL each router matches.

Key Points

  • Express takes the mount path off before the router matches. For GET /tickets/1, the app’s use entry for /tickets matches the URL’s first segment, /tickets, as a whole segment. Express keeps that part in req.baseUrl and hands the router the rest in req.url, which is /1. The list route "/" doesn’t match it, and the ID route "/:id" does, with req.params.id set to "1".
  • A route’s full URL is its mount path, then the router’s own path. That’s why the router’s paths leave the mount path out. Written as "/tickets/:id" inside the router, the ID route answers at /tickets/tickets/1, and /tickets/1 gets Express’s 404 page, Cannot GET /tickets/1.
  • A router can mount another router, and a mount path can hold a route parameter. The line ticketsRouter.use("/:id/notes", notesRouter); sends /tickets/1/notes on to the notes router. The app strips /tickets, and the tickets router gets /1/notes. Neither of its routes matches two segments, so the request reaches the new mount, which strips /1/notes and adds it to req.baseUrl. The notes router gets /, and its route matches.
  • A router starts with only the params from its own paths. The id was matched by the tickets router’s mount path, so the id param belongs to the tickets router. The notes router still sees /1 in its req.baseUrl, but not as a param, so its req.params was an empty object.
  • mergeParams: true passes the parent’s params down. Created with express.Router({ mergeParams: true }), the notes router’s req.params holds the ticket’s ID, the string "1", so it can tell which ticket the notes are for.

To predict a route’s full URL, add up the mount paths on the way down, then the route’s own path: /tickets + /:id/notes + / is /tickets/:id/notes. Each router strips its part before the next one matches, and a child router sees its parent’s params only when mergeParams is on.

The Final Code

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

00:00 Your ticket routes now live in a router, mounted at /tickets. In this video, you’ll follow a URL from the app into that router, and then into a router inside it.

00:12 Here’s why you’d want a router inside a router. Dev would like the fixers to leave notes on a ticket, such as Amara’s note that the toaster needs a new heating element.

00:22 Those notes belong under the ticket’s own URL. A notes router could answer them, but it needs to know which ticket they’re for. The notes are a toy for this video, so they won’t join the café’s API.

00:34 But a resource under another resource’s ID is a common reason to nest routers.

00:42 Start with a URL you already know. Send a GET for Priya’s toaster, ticket one. The app’s list has one entry, the tickets router at /tickets. The URL’s first segment is that mount path, so the app hands the request on.

00:58 As you saw in the last lesson, Express takes the mount path off first. It keeps that part in req.baseUrl, and the router gets the rest in req.url, which is /1.

01:10 The router tries its routes against that. The list route doesn’t match. The ID route does, with an ID of one.

01:20 So a route’s full URL is its mount path, then the router’s own path. That’s why the router’s paths leave the mount path out. Write the mount path into the router’s path, and the ID route moves to tickets, tickets, one, while the usual URL gets a 404.

01:38 Now give the notes a router. A new file, notes.ts, makes a router with one route, for its own root path. Its handler sends back the base URL, the URL, and the params that it sees.

01:53 Like the app, a router has a use method, so one new line in the tickets router mounts it. A mount path can hold a route parameter too, so this one is the ID, then notes.

02:06 Send a GET for ticket one’s notes. The app strips /tickets, as before, and the tickets router gets /1/notes. Neither route matches two segments, so the request reaches the new mount.

02:20 Its parameter matches the one, and Express strips /1/notes too, adding it to the base URL. The notes router gets a bare slash, and its route matches.

02:31 The base URL now holds everything stripped so far, and the URL is a slash. The params are still hidden. Your turn: pause the video and predict what the notes router finds in its params.

02:46 It’s an empty object. The one was matched by the tickets router’s mount path, so the ID param stays with the tickets router. Its text is in the base URL, but not as a param.

02:59 A router starts with only the params from its own paths, and the notes route’s path has none.

03:06 To pass the parent’s params down, give express.Router() an options object, with mergeParams set to true. Save, and send the same request again.

03:16 This time the params hold the ID, the string one. Now the notes router can tell which ticket the notes are for. A real notes route would look that ticket up first, and answer 404 when there isn’t one.

03:31 To predict a route’s full URL, add up the mount paths on the way down, then the route’s own path. Each router strips its part before the next one matches, and a child router sees its parent’s params only when mergeParams is on.

03:46 Next comes Exercise: Mount the Parts Router. There, you’ll give the café’s parts shelf a router of its own, answering at the shelf’s usual URLs.

Become a Member to join the conversation.