JavaScript Express Basics Parameters, Query Strings, and Modularizing Routes Using Data and Route Parameters

Ceil-Ian Maralit
Ceil-Ian Maralit
Front End Web Development Techdegree Graduate 19,288 Points

Request object params property

How did the value for the route parameter from the URL is stored in the request object params? Thank you!

1 Answer

Marco Cornejo
Marco Cornejo
3,377 Points

req.params is an object containing properties mapped to Route parameters, in other words, the Route parameters are simply keys being declared, and req.params assign a value to them.

Say we want to display card with id 6, the composition is as follows

//Route path: /card/:id
//Request URL: http://localhost:3000/cards/6
//req.params: { "id": "6" }

Note that req.params can store multiple key:values, which is how most search engines apply filters or redeem/referral urls work, for further details check the Documentation: https://expressjs.com/en/api.html#req.params

I hope I answered your question, cheers!