Update: req.param()
is now deprecated, so going forward do not use this answer.
Your answer is the preferred way to do it, however I thought I'd point out that you can also access url, post, and route parameters all with req.param(parameterName, defaultValue)
.
In your case:
var color = req.param('color');
From the express guide:
lookup is performed in the following order:
- req.params
- req.body
- req.query
Note the guide does state the following:
Direct access to req.body, req.params, and req.query should be favoured for clarity - unless you truly accept input from each object.
However in practice I've actually found req.param()
to be clear enough and makes certain types of refactoring easier.