# 響應(Response)
# 響應(Response)
Koa `Response` 對象是對 node 的 response 進一步抽象和封裝,提供了日常 HTTP 服務器開發中一些有用的功能。
## API
### res.header
Response header 對象。
### res.socket
Request socket。
### res.status
獲取 response status。不同于 node 在默認情況下 `res.statusCode` 為200,`res.status` 并沒有賦值。
### res.statusString
Response status 字符串。
### res.status=
通過 數字狀態碼或者不區分大小寫的字符串來設置response status:
- 100 "continue"
- 101 "switching protocols"
- 102 "processing"
- 200 "ok"
- 201 "created"
- 202 "accepted"
- 203 "non-authoritative information"
- 204 "no content"
- 205 "reset content"
- 206 "partial content"
- 207 "multi-status"
- 300 "multiple choices"
- 301 "moved permanently"
- 302 "moved temporarily"
- 303 "see other"
- 304 "not modified"
- 305 "use proxy"
- 307 "temporary redirect"
- 400 "bad request"
- 401 "unauthorized"
- 402 "payment required"
- 403 "forbidden"
- 404 "not found"
- 405 "method not allowed"
- 406 "not acceptable"
- 407 "proxy authentication required"
- 408 "request time-out"
- 409 "conflict"
- 410 "gone"
- 411 "length required"
- 412 "precondition failed"
- 413 "request entity too large"
- 414 "request-uri too large"
- 415 "unsupported media type"
- 416 "requested range not satisfiable"
- 417 "expectation failed"
- 418 "i'm a teapot"
- 422 "unprocessable entity"
- 423 "locked"
- 424 "failed dependency"
- 425 "unordered collection"
- 426 "upgrade required"
- 428 "precondition required"
- 429 "too many requests"
- 431 "request header fields too large"
- 500 "internal server error"
- 501 "not implemented"
- 502 "bad gateway"
- 503 "service unavailable"
- 504 "gateway time-out"
- 505 "http version not supported"
- 506 "variant also negotiates"
- 507 "insufficient storage"
- 509 "bandwidth limit exceeded"
- 510 "not extended"
- 511 "network authentication required"
**注意**:不用擔心記不住這些字符串,如果您設置錯誤,會有異常拋出,并列出該狀態碼表來幫助您進行更正。
### res.length=
通過給定值設置 response Content-Length。
### res.length
如果 Content-Length 作為數值存在,或者可以通過 `res.body` 來進行計算,則返回相應數值,否則返回 `undefined`。
### res.body
獲得 response body。
### res.body=
設置 response body 為如下值:
- `string` written
- `Buffer` written
- `Stream` piped
- `Object` json-stringified
- `null` no content response
如果 `res.status` 沒有賦值,Koa會自動設置為 `200` 或 `204`。
#### String
Content-Type 默認為 text/html 或者 text/plain,兩種默認 charset 均為 utf-8。 Content-Length 同時會被設置。
#### Buffer
Content-Type 默認為 application/octet-stream,Content-Length同時被設置。
#### Stream
Content-Type 默認為 application/octet-stream。
#### Object
Content-Type 默認為 application/json。
### res.get(field)
獲取 response header 中字段值,field 不區分大小寫。
```
var etag = this.get('ETag');
```
### res.set(field, value)
設置 response header 字段 `field` 的值為 `value`。
```
this.set('Cache-Control', 'no-cache');
```
### res.set(fields)
使用對象同時設置 response header 中多個字段的值。
```
this.set({
'Etag': '1234',
'Last-Modified': date
});
```
### res.remove(field)
移除 response header 中字段 `filed`。
### res.type
獲取 response `Content-Type`,不包含像 "charset" 這樣的參數。
```
var ct = this.type;
// => "image/png"
```
### res.type=
通過 mime 類型的字符串或者文件擴展名設置 response `Content-Type`
```
this.type = 'text/plain; charset=utf-8';
this.type = 'image/png';
this.type = '.png';
this.type = 'png';
```
注意:當可以根據 `res.type` 確定一個合適的 `charset` 時,`charset` 會自動被賦值。 比如 `res.type = 'html'` 時,charset 將會默認設置為 "utf-8"。然而當完整定義為 `res.type = 'text/html'`時,charset 不會自動設置。
### res.redirect(url, \[alt\])
執行 \[302\] 重定向到對應 `url`。
字符串 "back" 是一個特殊參數,其提供了 Referrer 支持。當沒有Referrer時,使用 `alt` 或者 `/` 代替。
```
this.redirect('back');
this.redirect('back', '/index.html');
this.redirect('/login');
this.redirect('http://google.com');
```
如果想要修改默認的 \[302\] 狀態,直接在重定向之前或者之后執行即可。如果要修改 body,需要在重定向之前執行。
```
this.status = 301;
this.redirect('/cart');
this.body = 'Redirecting to shopping cart';
```
### res.attachment(\[filename\])
設置 "attachment" 的 `Content-Disposition`,用于給客戶端發送信號來提示下載。filename 為可選參數,用于指定下載文件名。
### res.headerSent
檢查 response header 是否已經發送,用于在發生錯誤時檢查客戶端是否被通知。
### res.lastModified
如果存在 `Last-Modified`,則以 `Date` 的形式返回。
### res.lastModified=
以 UTC 格式設置 `Last-Modified`。您可以使用 `Date` 或 date 字符串來進行設置。
```
this.response.lastModified = new Date();
```
### res.etag=
設置 包含 `"`s 的 ETag。注意沒有對應的 `res.etag` 來獲取其值。
```
this.response.etag = crypto.createHash('md5').update(this.body).digest('hex');
```
### res.append(field, val)
在 header 的 `field` 后面 追加 `val`。
### res.vary(field)
相當于執行res.append('Vary', field)。