Nodejs, Express GET POST params

express, javascript, node.js

Solution

You're dealing with three different methods of parameter-passing:

1) Path parameters, which express's router captures in `req.param` when you use colon-prefixed components or regex captures in your route. These can be present in both GET and POST requests.

2) URL query-string parameters, which will be captured in `req.query` if you use the `express.query` middleware. These can also be present in both GET and POST requests.

3) Body parameters, which will be captured in `req.body` if you use the `express.bodyParser` middleware. These will only be present in POST requests that have a `Content-Type` of "x-www-form-urlencoded".

So what you need to do is to merge all three objects (if they exist) into one. There are no native `Object` methods for doing this, but there are lots of popular workarounds. For example, the underscore.js library defines an `extend` function, which would allow you to write

req.params=_.extend(req.params || {}, req.query || {}, req.body || {}).

If you don't want to use a library and want to roll your own way of extending objects, take a look at this blog post.

Problem

I'm new to Node/Express.. I see GET params can be captured like so: ``` app.get('/log/:name', api.logfunc); ``` POST like so: `app.post('/log', ...` (form variables available in req.body.) I'm aware of app.all, but is there a single way I can get all the variables for GET and POST when using app.all? (I'm too used to $_REQUEST in php!:) thx,

Original source