本文共 4563 字,大约阅读时间需要 15 分钟。
express req
Express.js is the most popular server framework because it provides a developer-friendly abstraction of the core http
module in Node.js. It’s like using jQuery instead of document.querySelectorAll
because it lets you, the developer, be way more productive!
Express.js是最受欢迎的服务器框架,因为它为Node.js中的核心http
模块提供了开发人员友好的抽象。 这就像使用jQuery而不是document.querySelectorAll
因为它使您(开发人员)更加高效!
The req
object in Express.js allows you to examine every aspect about the request that the client made (whether by browser, by cURL, or mobile browser, etc.). This encompasses lots of things like the URL, user agent string, JSON data, form data… A lot! In this post you’ll learn the basics about the req
object in Express.js.
Express.js中的req
对象使您可以检查有关客户端发出的请求的各个方面(无论是通过浏览器,通过cURL还是通过移动浏览器等)。 其中包含许多内容,例如URL,用户代理字符串,JSON数据,表单数据……很多! 在本文中,您将学习Express.js中有关req
对象的基础知识。
This article isn’t covering req
in exhaustive detail, but instead provides an overview of the most popular aspects of req
. And you can to learn about the response (res
) object.
本文并未详尽介绍req
,而是提供了req
最受欢迎方面的概述。 您可以以了解响应( res
)对象。
To view comprehensive docs for the req
object, visit the .
要查看req
对象的综合文档,请访问 。
There are three primary ways for Express.js apps to receive user-supplied data: req.params
, req.query
, and req.body
.
Express.js应用程序通过三种主要方式来接收用户提供的数据: req.params
, req.query
和req.body
。
req.params
(req.params
)// GET https://swamp.com/user/50d154c157981ef2app.get('/:userid', (req, res) => { console.log(req.params.userid) // "50d154c157981ef2"})
req.query
(req.query
)Access the query string in the URL. Many times you’ll see functionality for search, filtering, and sorting use query strings:
访问URL中的查询字符串。 很多时候,您会看到使用查询字符串进行搜索,过滤和排序的功能:
// GET https://swamp.com/search?keyword=louisiana-swampsapp.get('/search', (req, res) => { console.log(req.query.keyword) // "louisiana-swamps"})
req.body
(req.body
)Allows you to access the JSON data that was sent in the request. Generally used in POST/PUT requests to send arbitrary-length JSON to the server:
允许您访问请求中发送的JSON数据。 通常在POST / PUT请求中用于将任意长度的JSON发送到服务器:
// POST https://swamp.com/login//// {// "email": "sam@gmail.com",// "password": "chompz4lyfe"// }app.post('/login', (req, res) => { console.log(req.body.email) // "sam@gmail.com" console.log(req.body.password) // "chompz4lyfe"})
The following SVG graphic breaks down the anatomy of an URL:
以下SVG图形分解了URL的结构:
// https://mossy.swamp.com/alabama?filter=very-humidapp.get('/alabama', (req, res) => { console.log(req.protocol) // "https" console.log(req.hostname) // "swamp.com" console.log(req.path) // "/alabama" console.log(req.originalUrl) // "/alabama?filter=very-humid" console.log(req.subomains) // "['mossy']"})
You can easily access various parts of the URL using these built-in properties. Most of these are straight-forward except for req.subdomains
which actually gives you an array since you could have multiple subdomains. For example: https://secure.mossy.swamp.com
would return ['mossy', 'secure']
.
您可以使用这些内置属性轻松访问URL的各个部分。 除req.subdomains
之外,其中大多数都简单req.subdomains
,因为您可能有多个子域,所以实际上为您提供了一个数组。 例如: https://secure.mossy.swamp.com
: https://secure.mossy.swamp.com
将返回['mossy', 'secure']
。
req
属性 (Additional req
Properties)req.method
(req.method
)Access the HTTP method (GET, POST, PUT, DELETE) with req.method
.
使用req.method
访问HTTP方法(GET,POST,PUT,DELETE)。
app.delete('/', (req, res) => { console.log(req.method) // "DELETE"})
req.header()
(req.header()
)Access the headers sent in the request:
访问请求中发送的标头:
app.post('/login', (req, res) => { req.header('Content-Type') // "application/json" req.header('user-agent') // "Mozilla/5.0 (Macintosh Intel Mac OS X 10_8_5) AppleWebKi..." req.header('Authorization') // "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..."})
The argument for req.header
is case-insensitive so you can use req.header('Content-Type')
and req.header('content-type')
interchangeably.
req.header
的参数不区分大小写,因此您可以req.header('Content-Type')
使用req.header('Content-Type')
和req.header('content-type')
。
req.cookies
(req.cookies
)If you’re using to parse cookies it will store it in req.cookies
:
如果您使用来解析cookie,它将存储在req.cookies
:
// Cookie sessionDate=2019-05-28T01:49:11.968Zreq.cookies.sessionDate // "2019-05-28T01:49:11.968Z"
There you have it! These are the most popular ways that req
is used in Express.js. If you’d like to view the official documentation on req
please visit the .
你有它! 这些是Express.js中最常用的req
方法。 如果您想查看req
上的官方文档,请访问 。
翻译自:
express req
转载地址:http://ishgb.baihongyu.com/