Skip to content
This repository has been archived by the owner on May 3, 2018. It is now read-only.

Latest commit

 

History

History
37 lines (28 loc) · 1.16 KB

routing.md

File metadata and controls

37 lines (28 loc) · 1.16 KB

Routing

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 path: /users/:userId/books/:bookId
Request URL: http://localhost:3000/users/34/books/8989
req.params: { "userId": "34", "bookId": "8989" }

To define routes with route parameters, simply specify the route parameters in the path of the route as shown below.

server.get('/users/:userId/books/:bookId', (req, res) =>
{
    res.send(req.params)
})

Since the hyphen (-) and the dot (.) are interpreted literally, they can be used along with route parameters for useful purposes.

Route path: /flights/:from-:to
Request URL: http://localhost:3000/flights/LAX-SFO
req.params: { "from": "LAX", "to": "SFO" }
Route path: /plantae/:genus.:species
Request URL: http://localhost:3000/plantae/Prunus.persica
req.params: { "genus": "Prunus", "species": "persica" }

The name of route parameters must be made up of “word characters” ([A-Za-z0-9_]).