express取get参数_使用Express检索GET查询字符串参数

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

翻译自: https://flaviocopes.com/express-get-query-variables/

express取get参数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值