无涯教程-Node.js - Request Object函数

161 阅读2分钟

req 对象代表HTTP请求,并具有请求查询字符串,参数,正文,HTTP标头等的属性。

Request Object属性

以下是与请求对象关联的一些属性的列表。

Sr.No. Properties & 描述
1

req.app

此属性保存对使用中间件的快速应用程序实例的引用。

2

req.baseUrl 的

安装路由器实例的URL路径。

3

req.body

包含在请求正文中提交的键/值数据对。默认情况下,它是未定义的,并且在使用主体解析中间件(例如 body-parser )时填充

4

req.cookies

使用cookie解析器中间件时,此属性是一个包含请求发送的cookie的对象。

5

req.fresh

指示请求是否为"fresh"。与res.stale相反。

6

req.hostname

包含来自"主机" HTTP标头的主机名。

7

req.ip

请求的远程IP地址。

8

req.ips

当信任代理设置为true时,此属性包含在" X-Forwarded-For"请求标头中指定的IP地址数组。

9

req.originalUrl

这个属性很像req.url;但是,它保留了原始请求URL,允许您自由重写req.url以进行内部路由。

10

req.params

一个对象,该对象包含映射到命名路由"params"的属性。例如,如果您具有路由/user /:name,则" name"属性可以作为req.params.name使用。该对象默认为{}。

11

req.path

包含请求URL的路径部分。

12

req.protocol

使用TLS请求时,请求协议字符串" http"或" https"。

13

req.query

包含路由中每个查询字符串参数属性的对象。

14

req.route

当前匹配的路由,一个字符串。

15

req.secure

如果创建TLS连接,则为true的布尔值。

16

req.signedCookies

使用cookie解析器中间件时,此属性包含请求发送的已签名的cookie,未签名且可以使用。

17

req.stale

指示请求是否为"stale",与req.fresh相反。

18

req.subdomains

请求的域名中的子域数组。

19

req.xhr

一个布尔值,如果请求的" X-Requested-With"标头字段为" XMLHttpRequest",则为true,表示该请求是由客户端库(如jQuery)发出的。

req.accepts(types)

req.accepts(types)

此方法根据请求的"Accept HTTP header"字段检查指定的内容类型是否可接受。以下是一些示例-

//Accept: text/html
req.accepts(html);
//=> "html"

//Accept: text/*, application/json req.accepts(html);

//=> "html" req.accepts(text/html); //=> "text/html"

req.get(field)

req.get(field)

此方法返回指定的HTTP请求标头字段,以下是一些示例-

req.get(Content-Type);
//=> "text/plain"

req.get(content-type); //=> "text/plain"

req.get(Something); //=> undefined

req.is(type)

req.is(type)

如果传入请求的" Content-Type" HTTP标头字段与type参数指定的MIME类型匹配,则此方法返回true。以下是几个例子-

//With Content-Type: text/html; charset=utf-8
req.is(html);
req.is(text/html);
req.is(text/*);
//=> true

req.param(name[,defaultValue])

req.param(name[,defaultValue])

如果存在,此方法将返回参数名称的值。以下是几个例子-

//?name=tobi
req.param(name)
//=> "tobi"

//POST name=tobi req.param(name) //=> "tobi"

///user/tobi for /user/:name req.param(name) //=> "tobi"

参考链接

www.learnfk.com/nodejs/node…