node.js 为什么 req.params 返回一个空数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10523644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does req.params return an empty array?
提问by Shamoon
I'm using Node.js and I want to see all of the parameters that have been posted to my script. To get to my function, in my routes/index.jsI'm doing:
我正在使用 Node.js,我想查看已发布到我的脚本的所有参数。为了达到我的功能,routes/index.js我正在做:
app.post('/v1/order', order.create);
Then in my function, I have:
然后在我的函数中,我有:
exports.create = function(req, res, next) {
console.log( req.params );
But it's returning an empty array. But when I do:
但它返回一个空数组。但是当我这样做时:
exports.create = function(req, res, next) {
console.log( req.param('account_id') );
I get data. So I'm a bit confused as to what's going on here.
我得到数据。所以我对这里发生的事情有点困惑。
回答by Eddy
req.params
can only get the param of request url in this pattern:/user/:name
req.params
只能在这种模式下获取请求url的参数:/user/:name
req.query
get query params(name) like /user?name=123or body params.
req.query
获取查询参数(名称),例如/user?name=123或正文参数。
回答by Marius Kjeldahl
req.params only contain the route params, not query string params (from GET) and not body params (from POST). The param() function however checks all three, see:
req.params 只包含路由参数,不包含查询字符串参数(来自 GET)和正文参数(来自 POST)。然而 param() 函数会检查所有三个,请参阅:

