博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
express req_了解Express.js中的req对象
阅读量:2507 次
发布时间:2019-05-11

本文共 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对象的综合文档,请访问 。

用户提供的数据 (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/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"})

检查网址 (Examining the URL)

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"

结语 (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 .

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

翻译自:

express req

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

你可能感兴趣的文章
Laravel中的$loop
查看>>
CentOS7 重置root密码
查看>>
Centos安装Python3
查看>>
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Laravel框架学习笔记之任务调度(定时任务)
查看>>
laravel 定时任务秒级执行
查看>>
浅析 Laravel 官方文档推荐的 Nginx 配置
查看>>
Swagger在Laravel项目中的使用
查看>>
Laravel 的生命周期
查看>>
CentOS Docker 安装
查看>>
Nginx
查看>>
Navicat远程连接云主机数据库
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
MySQL innert join、left join、right join等理解
查看>>
sdc时序约束
查看>>
NoC片上网络
查看>>
开源SoC整理
查看>>
【2020-3-21】Mac安装Homebrew慢,解决办法
查看>>
influxdb 命令行输出时间为 yyyy-MM-dd HH:mm:ss(年月日时分秒)的方法
查看>>