请求参数 (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:
这些是您可能会使用的主要属性:
Property | Description |
---|---|
.app | holds a reference to the Express app object |
.baseUrl | the base path on which the app responds |
.body | contains the data submitted in the request body (must be parsed and populated manually before you can access it) |
.cookies | contains the cookies sent by the request (needs the cookie-parser middleware) |
.hostname | the hostname as defined in the Host HTTP header value |
.ip | the client IP |
.method | the HTTP method used |
.params | the route named parameters |
.path | the URL path |
.protocol | the request protocol |
.query | an object containing all the query strings used in the request |
.secure | true if the request is secure (uses HTTPS) |
.signedCookies | contains the signed cookies sent by the request (needs the cookie-parser middleware) |
.xhr | true 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月发布)和更高版本开始不再如此。