通过 req.query[myParam]
和 req.params.myParam
获取 QUERY_STRING 参数有区别吗?如果是这样,我应该什么时候使用哪个?
最佳答案
鉴于这条路线
app.get('/hi/:param1', function(req,res){} );
// regex version
app.get(/^\/hi\/(.*)$/, function(req,res){} );
// unnamed wild card
app.get('/hi/*', function(req,res){} );
并给出这个 URL
http://www.google.com/hi/there?qs1=you&qs2=tube
你将拥有:
req.查询
{
qs1: 'you',
qs2: 'tube'
}
req.参数
{
param1: 'there'
}
When you use a regular expression for the route definition, capture groups are provided in the array using
req.params[n]
, where n is the nth capture group. This rule is applied to unnamed wild card matches with string routes
关于Node.js:req.query[] 和 req.params 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14417592/