KOA 學習(三)

請求(Request)

Koa Request 對象是對 node 的 request 進一步抽象和封裝,提供了平常 HTTP 服務器開發中一些有用的功能。html

req.header 請求頭對象node

request.headers    req.header別名 git

req.method 請求方法github

req.method= 設置請求方法,在實現中間件時很是有用,好比 methodOverride()express

req.length 以數字的形式返回 request 的內容長度(Content-Length),或者返回 undefinedjson

req.url 得到請求url地址。數組

req.url= 設置請求地址,用於重寫url地址時。緩存

req.originalUrl 獲取請求原始地址。服務器

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

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 的值。

相關文章
相關標籤/搜索