当前位置: 首页 > 工具软件 > express.java > 使用案例 >

express req_了解Express.js中的req对象

徐涵亮
2023-12-01

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 read this post to learn about the response (res) object.

本文并未详尽介绍req ,而是提供了req最受欢迎方面的概述。 您可以阅读这篇文章,以了解响应( res )对象。

To view comprehensive docs for the req object, visit the official documentation.

要查看req对象的综合文档,请访问官方文档

用户提供的数据 (User-supplied Data)

There are three primary ways for Express.js apps to receive user-supplied data: req.params, req.query, and req.body.

Express.js应用程序通过三种主要方式来接收用户提供的数据: req.paramsreq.queryreq.body

req.params (req.params)

// GET https://swamp.com/user/50d154c157981ef2

app.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-swamps

app.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"
})

检查网址 (Examining the URL)

The following SVG graphic breaks down the anatomy of an URL:

以下SVG图形分解了URL的结构:

// https://mossy.swamp.com/alabama?filter=very-humid

app.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 cookie-parser to parse cookies it will store it in req.cookies:

如果您使用cookie解析器来解析cookie,它将存储在req.cookies

// Cookie sessionDate=2019-05-28T01:49:11.968Z

req.cookies.sessionDate // "2019-05-28T01:49:11.968Z"

结语 (Wrapping Up)

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 official Express.js docs.

你有它! 这些是Express.js中最常用的req方法。 如果您想查看req上的官方文档,请访问Express.js官方文档

翻译自: https://www.digitalocean.com/community/tutorials/nodejs-req-object-in-expressjs

express req

 类似资料: