博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Express,请求参数
阅读量:2509 次
发布时间:2019-05-11

本文共 3696 字,大约阅读时间需要 12 分钟。

请求参数 (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 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
属性 描述
.app 持有对Express应用程序对象的引用
.baseUrl 应用程序响应的基本路径
。身体 包含在请求正文中提交的数据(必须先对其进行解析和填充,然后才能访问它)
。饼干 包含请求发送的cookie-parser (需要cookie-parser中间件)
。主机名 值中定义的名
.ip 客户端IP
。方法 使用的HTTP方法
.params 路线命名参数
。路径 URL路径
。协议 请求协议
。查询 包含请求中使用的所有查询字符串的对象
。安全 如果请求是安全的(使用HTTPS),则为true
.signedCookies 包含请求发送的签名cookie(需要cookie-parser中间件)
.xhr 如果请求是则返回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 //flavioreq.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 , using Content-Type: application/json, you will use the express.json() middleware:

如果使用Content-Type: application/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月发布)和更高版本开始不再如此。

翻译自:

转载地址:http://aqqgb.baihongyu.com/

你可能感兴趣的文章
删除了Ubuntu之后,不能正常进入到Win7系统之中的解决办法
查看>>
写一个正则表达式匹配手机号
查看>>
Linux试题
查看>>
TableLock插件
查看>>
java 获取页面中的 a 标签 的 href 实例
查看>>
Knowledge Point 20180305 详解精度问题
查看>>
开发 Windows 8 Bing地图应用(4)
查看>>
mysql-python安装时mysql_config not found
查看>>
loadrunner 场景设计-添加Unix、Linux Resources计数器
查看>>
Python 基于python编写一些算法程序等
查看>>
Python 一键commit文件、目录到SVN服务器
查看>>
毕业5年决定你的命运 ----值得所有不甘平庸的人看看
查看>>
基于Python的接口自动化-01
查看>>
前台返回json数据的常用方式+常用的AJAX请求后台数据方式
查看>>
spring boot下MultipartHttpServletRequest如何提高上传文件大小的默认值
查看>>
css继承和边框圆角 及 写三角形
查看>>
编译opencv有关cuda的代码
查看>>
spring quartz job autowired 出错 null pointer
查看>>
openfire 安装部署
查看>>
数据库查询某一字段为空的数据
查看>>