Express,请求参数

请求参数 (Request parameters)

I mentioned how the Request object holds all the HTTP request information.

我提到了Request对象如何保存所有HTTP请求信息。

These are the main properties you’ll likely use:

这些是您可能会使用的主要属性:

PropertyDescription
.appholds a reference to the Express app object
.baseUrlthe base path on which the app responds
.bodycontains the data submitted in the request body (must be parsed and populated manually before you can access it)
.cookiescontains the cookies sent by the request (needs the cookie-parser middleware)
.hostnamethe hostname as defined in the Host HTTP header value
.ipthe client IP
.methodthe HTTP method used
.paramsthe route named parameters
.paththe URL path
.protocolthe request protocol
.queryan object containing all the query strings used in the request
.securetrue if the request is secure (uses HTTPS)
.signedCookiescontains the signed cookies sent by the request (needs the cookie-parser middleware)
.xhrtrue if the request is an XMLHttpRequest
属性 描述
.app 持有对Express应用程序对象的引用
.baseUrl 应用程序响应的基本路径
。身体 包含在请求正文中提交的数据(必须先对其进行解析和填充,然后才能访问它)
。饼干 包含请求发送的cookie-parser (需要cookie-parser中间件)
。主机名 主机HTTP标头值中定义的主机
.ip 客户端IP
。方法 使用的HTTP方法
.params 路线命名参数
。路径 URL路径
。协议 请求协议
。查询 包含请求中使用的所有查询字符串的对象
。安全 如果请求是安全的(使用HTTPS),则为true
.signedCookies 包含请求发送的签名cookie(需要cookie-parser中间件)
.xhr 如果请求是XMLHttpRequest,则返回true

如何使用Express检索GET查询字符串参数 (How to retrieve the GET query string parameters using Express)

The query string is the part that comes after the URL path, and starts with a question mark ?.

查询字符串是URL路径之后的部分,并以问号?开头?

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检索POST查询字符串参数 (How to retrieve the POST query string parameters using Express)

POST query parameters are sent by HTTP clients for example by forms, or when performing a POST request sending data.

POST查询参数由HTTP客户端(例如,通过表单)发送,或在执行POST请求发送数据时发送。

How can you access this data?

您如何访问此数据?

If the data was sent as JSON, using Content-Type: application/json, you will use the express.json() middleware:

如果使用Content-Type: application/json将数据作为JSON发送,则将使用express.json()中间件:

const express = require('express')
const app = express()

app.use(express.json())

If the data was sent using Content-Type: application/x-www-form-urlencoded, you will need to use the express.urlencoded() middleware:

如果数据是使用Content-Type: application/x-www-form-urlencoded ,则需要使用express.urlencoded()中间件:

const express = require('express')
const app = express()

app.use(express.urlencoded({
  extended: true
}))

In both cases you can access the data by referencing it from Request.body:

在这两种情况下,都可以通过从Request.body引用数据来访问数据:

app.post('/form', (req, res) => {
  const name = req.body.name
})

Note: older Express versions required the use of the body-parser module to process POST data. This is no longer the case as of Express 4.16 (released in September 2017) and later versions.

注意:较早的Express版本需要使用body-parser模块来处理POST数据。 从Express 4.16(2017年9月发布)和更高版本开始不再如此。

翻译自: https://flaviocopes.com/express-request-parameters/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值