Lesson 5 / 23

Route Parameters

Capture dynamic segments from the URL path.

:name syntax

A colon before a path segment, like :id, marks it as a parameter. Express captures it into req.params.

Reading a param

GET /users/42 gives req.params.id === '42' — always a string.

app.get('/users/:id', (req, res) => {
  const { id } = req.params;
  res.json({ userId: id });
});

Multiple params

A path can have several: /users/:userId/posts/:postId fills both req.params.userId and req.params.postId.