表达js请求问题,为查询键传递多个值/集合

问题描述 投票:1回答:1

我需要传递多个值,即Express中特定查询键的集合。

我的记忆可能会欺骗我,但我倾向于相信我在网上的某个地方看到过类似的东西:

http://<somehost>/<somepath>?id[]=10&id[]=11&id[]=12

我知道Ruby on Rails将这些querystring值作为数组返回,但我认为express有一些类似的querystring行为,但我在他们的文档中找不到它。

我错过了什么吗?

node.js express
1个回答
0
投票

与此同时,我发现它确实适用于快递。

这个查询字符串

http://<somehost>/<somepath>?id[]=10&id[]=11&id[]=12

被快递js解析成数组。从而

req.query.id => ["10","11","12"]

这应该在快速文档中提到。

实际上express使用名为'qs'的包,值得仔细研究:

https://github.com/ljharb/qs

它甚至可以做深层嵌套

http://<somehost>/<somepath>?persona[country][birth]=us&person[country][resident]=de

正在被解析成

{ person: { country: { birth: 'us', resident: 'de' } } }
© www.soinside.com 2019 - 2024. All rights reserved.