koa2学习笔记:request对象各属性含义,url相关属性区别

蛰伏已久 蛰伏已久 2019-09-03

Request对象下的大部分属性,也可以通过ctx直接进行访问

如 ctx.request.method 和 ctx.method是等价的

以下访问器和 Request 别名等效:

  • ctx.header

  • ctx.headers

  • ctx.method

  • ctx.method=

  • ctx.url

  • ctx.url=

  • ctx.originalUrl

  • ctx.origin

  • ctx.href

  • ctx.path

  • ctx.path=

  • ctx.query

  • ctx.query=

  • ctx.querystring

  • ctx.querystring=

  • ctx.host

  • ctx.hostname

  • ctx.fresh

  • ctx.stale

  • ctx.socket

  • ctx.protocol

  • ctx.secure

  • ctx.ip

  • ctx.ips

  • ctx.subdomains

  • ctx.is()

  • ctx.accepts()

  • ctx.acceptsEncodings()

  • ctx.acceptsCharsets()

  • ctx.acceptsLanguages()

  • ctx.get()



request.header 和 request.headers等价

返回请求header信息,如accept、host、cache-control、user-agent、cookie等

{ 
  host: 'localhost:3031',
  connection: 'keep-alive',
  pragma: 'no-cache',
  'cache-control': 'no-cache',
  'upgrade-insecure-requests': '1',
  'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
  'sec-fetch-mode': 'navigate',
  'sec-fetch-user': '?1',
  accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
  'sec-fetch-site': 'none',
  'accept-encoding': 'gzip, deflate, br',
  'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
  cookie: 'uuid=4' 
}

request.method

返回请求方式,大写“GET”、“POST”等


request url相关

假如用户访问网址为:http://localhost:3031/api/site/login?redirect=/&from=baidu

则各属性值分别如下

属性解释
url/api/site/login?redirect=/&from=baiduorigin后面的全部字符
originalUrl

/api/site/login?redirect=/&from=baidu


origin http://localhost:3031获取URL的来源,包括 protocol 和 host
href

http://localhost:3031/api/site/login?redirect=/&from=baidu

全部网址
path/api/site/loginorigin和参数之间,也就是我们定义的路由路径
querystring

redirect=/&from=baid

根据 ? 获取原始查询字符串,问号后面的字符
search

?redirect=/&from=baidu

使用 ? 获取原始查询字符串,包含问号及后面的字符
host

localhost:3031

域名和端口号(hostname:port)
hostnamelocalhost域名部分
query

{ redirect: '/', from: 'baidu' }

针对查询字符串解析后的对象, 当没有查询字符串时,返回一个空对象。请注意,不支持嵌套解析。
protocolhttp返回请求协议,“https” 或 “http”。当 app.proxy 是 true 时支持 X-Forwarded-Proto
securefalse通过 ctx.protocol == "https" 来检查请求是否通过 TLS 发出。


request.charset

不能使用ctx.charset获取

在存在时获取请求字符集,或者 undefined

ctx.request.charset;
// => "utf-8"


content-type相关

request.type

获取请求 Content-Type 不含参数 "charset"。

const ct = ctx.request.type;
// => "image/png"

request.is(types...)

检查传入请求是否包含 Content-Type 头字段, 并且包含任意的 mime type。 如果没有请求主体,返回 null。 如果没有内容类型,或者匹配失败,则返回 false。 反之则返回匹配的 content-type。

// 使用 Content-Type: text/html; charset=utf-8
ctx.is('html'); // => 'html'
ctx.is('text/html'); // => 'text/html'
ctx.is('text/*', 'text/html'); // => 'text/html'

// 当 Content-Type 是 application/json 时
ctx.is('json', 'urlencoded'); // => 'json'
ctx.is('application/json'); // => 'application/json'
ctx.is('html', 'application/*'); // => 'application/json'

ctx.is('html'); // => false


request.accepts(types)

检查给定的 type(s) 是否可以接受,如果 true,返回最佳匹配,否则为 false。 type 值可能是一个或多个 mime 类型的字符串,如 application/json,扩展名称如 json,或数组 ["json", "html", "text/plain"]

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

// Accept: text/*, application/json
ctx.accepts('html');
// => "html"
ctx.accepts('text/html');
// => "text/html"
ctx.accepts('json', 'text');
// => "json"
ctx.accepts('application/json');
// => "application/json"

// Accept: text/*, application/json
ctx.accepts('image/png');
ctx.accepts('png');
// => false

// Accept: text/*;q=.5, application/json
ctx.accepts(['html', 'json']);
ctx.accepts('html', 'json');
// => "json"

// No Accept header
ctx.accepts('html', 'json');
// => "html"
ctx.accepts('json', 'html');
// => "json"

你可以根据需要多次调用 ctx.accepts(),或使用 switch:

switch (ctx.accepts('json', 'html', 'text')) {
  case 'json': break;
  case 'html': break;
  case 'text': break;
  default: ctx.throw(406, 'json, html, or text only');
}

request.acceptsEncodings(encodings)

检查 encodings 是否可以接受,返回最佳匹配为 true,否则为 false。 请注意,您应该将identity 作为编码之一!

// Accept-Encoding: gzip
ctx.acceptsEncodings('gzip', 'deflate', 'identity');
// => "gzip"

ctx.acceptsEncodings(['gzip', 'deflate', 'identity']);
// => "gzip"

当没有给出参数时,所有接受的编码将作为数组返回:

// Accept-Encoding: gzip, deflate
ctx.acceptsEncodings();
// => ["gzip", "deflate", "identity"]

请注意,如果客户端显式地发送 identity;q=0,那么 identity 编码(这意味着没有编码)可能是不可接受的。 虽然这是一个边缘的情况,你仍然应该处理这种方法返回 false 的情况。

request.acceptsCharsets(charsets)

检查 charsets 是否可以接受,在 true 时返回最佳匹配,否则为 false

// Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5
ctx.acceptsCharsets('utf-8', 'utf-7');
// => "utf-8"

ctx.acceptsCharsets(['utf-7', 'utf-8']);
// => "utf-8"

当没有参数被赋予所有被接受的字符集将作为数组返回:

// Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5
ctx.acceptsCharsets();
// => ["utf-8", "utf-7", "iso-8859-1"]

request.acceptsLanguages(langs)

检查 langs 是否可以接受,如果为 true,返回最佳匹配,否则为 false

// Accept-Language: en;q=0.8, es, pt
ctx.acceptsLanguages('es', 'en');
// => "es"

ctx.acceptsLanguages(['en', 'es']);
// => "es"

当没有参数被赋予所有接受的语言将作为数组返回:

// Accept-Language: en;q=0.8, es, pt
ctx.acceptsLanguages();
// => ["es", "pt", "en"]


分享到

点赞(0)