Node.js: Difference between req.query[] and req.params


Question

Is there a difference between obtaining QUERY_STRING arguments via req.query[myParam] and req.params.myParam? If so, when should I use which?

Thanks.

1
95
1/19/2013 6:46:33 PM

Accepted Answer

req.params contains route parameters (in the path portion of the URL), and req.query contains the URL query parameters (after the ? in the URL).

You can also use req.param(name) to look up a parameter in both places (as well as req.body), but this method is now deprecated.

107
3/24/2017 4:33:35 PM

Given this route

app.get('/hi/:param1', function(req,res){} );

and given this URL http://www.google.com/hi/there?qs1=you&qs2=tube

You will have:

req.query

{
  qs1: 'you',
  qs2: 'tube'
}

req.params

{
  param1: 'there'
}

Express req.params >>


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon