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,
You’re dealing with three different methods of parameter-passing:
1) Path parameters, which express’s router captures in
req.paramwhen 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.queryif you use theexpress.querymiddleware. These can also be present in both GET and POST requests.3) Body parameters, which will be captured in
req.bodyif you use theexpress.bodyParsermiddleware. These will only be present in POST requests that have aContent-Typeof “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
Objectmethods for doing this, but there are lots of popular workarounds. For example, the underscore.js library defines anextendfunction, which would allow you to writeIf 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.