16.7k views
1 vote
How to access the route params in express

Option 1: req.params
Option 2: req.query
Option 3: req.routeParams
Option 4: req.body

1 Answer

6 votes

Final answer:

In Express, route parameters are accessed using req.params. This property contains the values captured from the URL, as defined by the route pattern.

Step-by-step explanation:

To access the route parameters in Express, you use req.params. These are the parts of the URL that are captured by the colon : prefix in your route definitions. For instance, if you have a route defined as /users/:userId and you navigate to /users/123, to get the value 123, your code would read req.params.userId.

It's important to note that req.query is used for accessing query string parameters, req.body is for accessing data sent in the body of the request (typically with POST or PUT requests), and req.routeParams doesn't exist in Express, hence it is not the correct option.

User Nelva
by
7.5k points