hanfeisun hanfeisun - 5 months ago 14
Node.js Question

How to access the GET parameters after "?" in Express?

I know how to get the params for queries like this:

app.get('/sample/:id', routes.sample);


In this case, I can use
req.params.id
to get the parameter (e.g.
2
in
/sample/2
).

However, for url like
/sample/2?color=red
, how can I access the variable
color
?

I tried
req.params.color
but it didn't work.

Answer

So, after checking out the express reference, I found that req.query.color would return me the value I'm looking for.