Route Parameters

Route Parameters

  • Route parameters are named URL segments that are used to capture the values specified at their position in the URL

  • The captured values are populated in the req.params object, with the name of the route parameter specified in the path as their respective keys

  • Route parameters are similar to function parameters

  • Given the following route handler:

    app.get('/users/:userId/books/:bookId', function (req, res) {
      res.send(req.params)
    })
    
    • If the client makes a request to the following url: GET http://localhost:3000/users/34/books/8989

    • Then inside our route handle req.params will contain the following object: { "userId": "34", "bookId": "8989" }