express取get参数
The query string is the part that comes after the URL path, and starts with a question mark ?
.
查询字符串是URL路径之后的部分,并以问号?
开头?
。
For example:
例如:
?name=flavio
Multiple query parameters can be added using &
:
可以使用&
添加多个查询参数:
?name=flavio&age=35
How do you get those query string values in Express?
如何在Express中获取那些查询字符串值?
Express makes it very easy by populating the Request.query
object for us:
Express通过为我们填充Request.query
对象使它变得非常容易:
const express = require('express')
const app = express()
app.get('/', (req, res) => {
console.log(req.query)
})
app.listen(8080)
This object is filled with a property for each query parameter.
该对象由每个查询参数的属性填充。
If there are no query params, it’s an empty object.
如果没有查询参数,则为空对象。
This makes it easy to iterate on it using the for…in loop:
这使得使用for…in循环可以很容易地对其进行迭代:
for (const key in req.query) {
console.log(key, req.query[key])
}
This will print the query property key and the value.
这将打印查询属性键和值。
You can access single properties as well:
您还可以访问单个属性:
req.query.name //flavio
req.query.age //35
express取get参数