The difference between req.params and req.query

Asked

Viewed 5,329 times

4

I’m having a question about url parameters. Is the following, I know there’s a way I can rescue a parameter this way users/:id, Then I pick up with the req.params.id, but I saw a video that the guy used this way, req.query and in that req.query he placed inside a variable, and all parameters he passed via url he managed to rescue.

My question is: what is the difference between them? And in what context should I use them??

2 answers

4

Edgar, as far as I know, params are the parameters of your endpoint URL, while query are the parameters sent after query(?).

For example, in this URL, it would look like this:

http://www.test.com/xpto/? shale=1

params : exposure

query : shale=1

  • If you’re using express, this documentation of them is pretty cool: http://expressjs.com/en/api.html

0

Normally query parameters are used for queries/filter. For example let’s assume that your application works with companies and employees.

We could take the following route:

GET /companies/1/employees ( return all company employees 1 )

GET /companies/1/employees? assets=true ( return all employees of company 1 who are active )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.