admin管理员组文章数量:1194134
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,
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,
Share Improve this question asked Jul 26, 2012 at 3:49 C BC B 13.3k5 gold badges38 silver badges48 bronze badges 1 |2 Answers
Reset to default 24You'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.
Personnaly, i merge req.params
, req.body
, req.query
in one single object req.props
with Object.assign()
in ES6:
You just need to write this in your route:
app.all('/myroute/:myparam', (req, res, next) => {
// merge all req data in one
req.props = Object.assign(req.query, req.params, req.body);
// optional :
// delete req.query;
// delete req.params;
// delete req.body;
});
In ES5:
app.all('/myroute/:myparam', function(req, res, next){
// merge all req data in one
req.props = {};
if(req.query) for (var attrname in req.query) { req.props[attrname] = req.query[attrname]; }
if(req.params) for (var attrname in req.params) { req.props[attrname] = req.params[attrname]; }
if(req.body) for (var attrname in req.body) { req.props[attrname] = req.body[attrname]; }
// optional :
// delete req.query;
// delete req.params;
// delete req.body;
});
Now, you can access easily to your GET, POST, PUT params with req.props
in all your routes, be careful about similars name if you decide to delete the old req.
Also, you can do a middleware/functions to make it more useful yet.
More about Object.assign()
: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Object/assign
本文标签: JavaScriptNodeJSExpress GET POST paramsStack Overflow
版权声明:本文标题:javascript - Nodejs, Express GET POST params - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738498702a2090139.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
JSON.stringify(req.query)
forget
andJSON.stringify(req.body)
forpost
assuming youapp.use(express.bodyParser())
– Jonathan Ong Commented Jul 26, 2012 at 4:21