KOA 学习(三)

请求(Request)

Koa Request 对象是对 node 的 request 进一步抽象和封装,提供了日常 HTTP 服务器开发中一些有用的功能。

req.header 请求头对象

request.headers    req.header别名 

req.method 请求方法

req.method= 设置请求方法,在实现中间件时非常有用,比如 methodOverride()

req.length 以数字的形式返回 request 的内容长度(Content-Length),或者返回 undefined

req.url 获得请求url地址。

req.url= 设置请求地址,用于重写url地址时。

req.originalUrl 获取请求原始地址。

request.origin  获取求原始地址包括protocol and host.

ctx.request.origin
// => http://example.com

 req.path 获取请求路径名。

req.path= 设置请求路径名,并保留请求参数(就是url中?后面的部分)。

request.href 全路径,包括host和url

req.querystring 获取查询参数字符串(url中?后面的部分),不包含 ?。

req.querystring= 设置查询参数。 

req.search= 设置查询参数字符串。

req.host 获取 host (hostname:port)。 当 app.proxy 设置为 true 时,支持 X-Forwarded-Host

req.hostname 获取 hostname。当 app.proxy 设置为 true 时,支持 X-Forwarded-Host

req.type 获取请求 Content-Type,不包含像 "charset" 这样的参数。

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

req.charset 获取请求 charset,没有则返回 undefined:

req.query 

将查询参数字符串进行解析并以对象的形式返回,如果没有查询参数字字符串则返回一个空对象。

注意:该方法支持嵌套解析。

req.query= 根据给定的对象设置查询参数字符串。

ctx.query = { next: '/login' }

req.fresh 检查请求缓存是否 "fresh"(内容没有发生变化)。该方法用于在 If-None-Match / ETagIf-Modified-Since 和 Last-Modified 中进行缓存协调。当在 response headers 中设置一个或多个上述参数后,该方法应该被使用。

// freshness check requires status 20x or 304
ctx.status = 200;
ctx.set('ETag', '123');

// cache is ok
if (ctx.fresh) {
  ctx.status = 304;
  return;
}

// cache is stale
// fetch new data
ctx.body = yield db.find('something');

req.stale 与 req.fresh 相反。

req.protocol 返回请求协议,"https" 或者 "http"。 当 app.proxy 设置为 true 时,支持 X-Forwarded-Host

req.secure 简化版 this.protocol == "https",用来检查请求是否通过 TLS 发送。

req.ip 请求远程地址。 当 app.proxy 设置为 true 时,支持 X-Forwarded-Host

req.ips 当 X-Forwarded-For 存在并且 app.proxy 有效,将会返回一个有序(从 upstream 到 downstream)ip 数组。 否则返回一个空数组。

req.subdomains 以数组形式返回子域名。

req.is(type) 检查请求所包含的 "Content-Type" 是否为给定的 type 值。 如果没有 request body,返回 undefined。 如果没有 content type,或者匹配失败,返回 false。 否则返回匹配的 content-type。

比如说您希望保证只有图片发送给指定路由:

if (ctx.is('image/*')) {
  // process
} else {
  ctx.throw(415, 'images only!');
}

Content Negotiation

Koa request 对象包含 content negotiation 功能(由 accepts 和 negotiator 提供):

  • req.accepts(types)
  • req.acceptsEncodings(types)
  • req.acceptsCharsets(charsets)
  • req.acceptsLanguages(langs)

如果没有提供 types,将会返回所有的可接受类型。

如果提供多种 types,将会返回最佳匹配类型。如果没有匹配上,则返回 false,您应该给客户端返回 406 "Not Acceptable"

为了防止缺少 accept headers 而导致可以接受任意类型,将会返回第一种类型。因此,您提供的类型顺序非常重要。

req.accepts(types)

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

req.acceptsEncodings(encodings)

检查 encodings 是否可以被接受,当为 true 时返回最佳匹配,否则返回 false。 注意:您应该在 encodings 中包含 identity

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

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

req.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"

req.socket

返回请求的socket。

req.get(field)

返回请求 header 中对应 field 的值。

原文地址:https://www.cnblogs.com/myzy/p/6510522.html