响应(Response)

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

response.header

Response header 对象。

response.socket

response.status

获取 response status。不同于 node 在默认情况下 res.statusCode 为200,res.status 并没有赋值。

response.status=

通过 数字状态码或者不区分大小写的字符串来设置response status:

response.message

Get response status message. By default, response.message is associated with response.status.

response.message=

Set response status message to the given value.

response.length=

通过给定值设置 response Content-Length。

response.length

如果 Content-Length 作为数值存在,或者可以通过 res.body 来进行计算,则返回相应数值,否则返回 undefined

response.body

获得 response body。

response.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。

response.get(field)

获取 response header 中字段值,field 不区分大小写。

response.set(field, value)

ctx.set('Cache-Control', 'no-cache');

response.append(field, value)

ctx.append('Link', '<http://127.0.0.1/>');

res.set(fields)

使用对象同时设置 response header 中多个字段的值。

ctx.set({
'Etag': '',
'Last-Modified': date
});

res.remove(field)

移除 response header 中字段 filed

res.type

获取 response Content-Type,不包含像 "charset" 这样的参数。

response.type=

通过 mime 类型的字符串或者文件扩展名设置 response Content-Type

response.is(types...)

const minify = require('html-minifier');

app.use(function * minifyHTML(next) {
yield next; if (!ctx.response.is('html')) return; let body = ctx.body;
if (!body || body.pipe) return; if (Buffer.isBuffer(body)) body = body.toString();
ctx.body = minify(body);
});

res.redirect(url, [alt])

执行 [302] 重定向到对应 url

字符串 "back" 是一个特殊参数,其提供了 Referrer 支持。当没有Referrer时,使用 alt 或者 / 代替。

ctx.redirect('back');
ctx.redirect('back', '/index.html');
ctx.redirect('/login');
ctx.redirect('http://google.com');

如果想要修改默认的 [302] 状态,直接在重定向之前或者之后执行即可。如果要修改 body,需要在重定向之前执行。

ctx.status = ;
ctx.redirect('/cart');
ctx.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)。

最新文章

  1. H5 canvas的 width、height 与style中宽高的区别
  2. Android基于mAppWidget实现手绘地图(三)--环境搭建
  3. 学习OpenCV——绘制彩色直方图(HSV2BGR)
  4. BZOJ1003 物流运输 最短路+DP
  5. matlab练习程序(构造简单多边形)
  6. FlexNOC
  7. IE8-下背景色半透明滤镜在jquery动画中失效问题记录
  8. 在node.js中使用ejs的demo 第五篇
  9. Topcoder SRM 628 DIV 2
  10. 第一个python爬虫程序
  11. 201521123100 《Java程序设计》 第1周学习总结
  12. web项目错误页面友好处理404,500等
  13. EditTable-V1.0--续集
  14. github总结(4)--关于git reset --hard这个命令的惨痛教训
  15. Java 读取 Json格式的 内容
  16. 【转】tars源码漫谈第1篇------tc_loki.h (牛逼哄哄的loki库)
  17. 给 datepicker 设定日期格式
  18. Windows Azure 搭建网络代理 Proxy
  19. layer插件layer.photos()动态插入的图片无法正常显示
  20. 【svn】svn的使用

热门文章

  1. day 84 Vue学习之vue-cli脚手架下载安装及配置
  2. 网络安全系列 之 SQL注入学习总结
  3. 通过JBOSS服务器来实现JMS消息传送
  4. C++的注释
  5. iOS开发系列-NSURLConnection
  6. C++ 系列:typedef 和 #define 的区别
  7. html--伪等高布局
  8. 扩展kmp板子
  9. 廖雪峰Java14Java操作XML和JSON-2JSON-1Json介绍
  10. Google 打算用 QUIC 协议替代 TCP/UDP