Node.js:req.query[] 和 req.params 之间的区别

标签 node.js query-string

通过 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

Express req.params >>

关于Node.js:req.query[] 和 req.params 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14417592/

相关文章:

Node.js - session 不会通过 res.redirect() 持续存在

javascript - 分解 JS 函数

c# - C# 上带有特殊字符的查询字符串问题

javascript - 使用 ajax 时,某些问题页面有一个查询字符串

jquery - 如何在加载后删除部分 URL

javascript - 使用 node.js 将值传递到代理页面

javascript - 如何在 typescript 中使用联合类型

ios - 如何使用 FCM 在 ios 推送通知中播放自定义通知声音

java - 数字 jax-rs 的溢出保护

get - Concrete5:我可以在常规页面上使用 $_GET 变量作为查询字符串吗?