响应(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)

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

response.append(field, value)

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

res.set(fields)

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

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

res.remove(field)

移除 response header 中字段 filed

res.type

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

response.type=

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

response.is(types...)

  1. const minify = require('html-minifier');
  2.  
  3. app.use(function * minifyHTML(next) {
  4. yield next;
  5.  
  6. if (!ctx.response.is('html')) return;
  7.  
  8. let body = ctx.body;
  9. if (!body || body.pipe) return;
  10.  
  11. if (Buffer.isBuffer(body)) body = body.toString();
  12. ctx.body = minify(body);
  13. });

res.redirect(url, [alt])

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

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

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

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

  1. ctx.status = ;
  2. ctx.redirect('/cart');
  3. 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 字符串来进行设置。

  1. this.response.lastModified = new Date();

res.etag=

设置 包含 "s 的 ETag。注意没有对应的 res.etag 来获取其值。

  1. 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)。

KOA 学习(四)的更多相关文章

  1. TweenMax动画库学习(四)

    目录            TweenMax动画库学习(一)            TweenMax动画库学习(二)            TweenMax动画库学习(三)            Tw ...

  2. Koa 学习笔记

    开始 就像官网上说的,一切框架都从一个"Hello World"开始,首先我们新建一个 package.json,内容尽量简单: { "name": " ...

  3. koa 学习资料

    koa 学习资料 学习资料 地址 koa 中文版 https://koa.bootcss.com/

  4. SVG 学习<四> 基础API

    目录 SVG 学习<一>基础图形及线段 SVG 学习<二>进阶 SVG世界,视野,视窗 stroke属性 svg分组 SVG 学习<三>渐变 SVG 学习<四 ...

  5. Android JNI学习(四)——JNI的常用方法的中文API

    本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...

  6. SCARA——OpenGL入门学习四(颜色)

    OpenGL入门学习[四] 本次学习的是颜色的选择.终于要走出黑白的世界了~~ OpenGL支持两种颜色模式:一种是RGBA,一种是颜色索引模式. 无论哪种颜色模式,计算机都必须为每一个像素保存一些数 ...

  7. ZigBee学习四 无线+UART通信

    ZigBee学习四 无线+UART通信 1) 协调器编程 修改coordinator.c文件 byte GenericApp_TransID; // This is the unique messag ...

  8. (转)SpringMVC学习(四)——Spring、MyBatis和SpringMVC的整合

    http://blog.csdn.net/yerenyuan_pku/article/details/72231763 之前我整合了Spring和MyBatis这两个框架,不会的可以看我的文章MyBa ...

  9. Spring Boot 项目学习 (四) Spring Boot整合Swagger2自动生成API文档

    0 引言 在做服务端开发的时候,难免会涉及到API 接口文档的编写,可以经历过手写API 文档的过程,就会发现,一个自动生成API文档可以提高多少的效率. 以下列举几个手写API 文档的痛点: 文档需 ...

  10. Expression Blend学习四控件

    原文:Expression Blend学习四控件 Expression Blend制作自定义按钮 1.从Blend工具箱中添加一个Button,按住shift,将尺寸调整为125*125; 2.右键点 ...

随机推荐

  1. PAT甲级——A1123 Is It a Complete AVL Tree【30】

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  2. PAT甲级——A1110 Complete Binary Tree【25】

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...

  3. 出现java.lang.NoClassDefFoundError: org/apache/commons/collections/FastHashMap错误问题解决

    首先出现这个问题,你应该是用了 BeanUtils.populate(meter,map); import org.apache.commons.beanutils.BeanUtils;并且导入了co ...

  4. node环境下安装vue-cli

    一. node安装 1)如果不确定自己是否安装了node,可以在命令行工具内执行: node -v  (检查一下 版本): 2)如果 执行结果显示: xx 不是内部命令,说明你还没有安装node , ...

  5. USACO 2012 March Silver Tractor /// 优先队列BFS oj21567

    题目大意: 输入n,(x,y):n为阻挡的草堆数量,(x,y)为开始时拖拉机所在的位置 接下来n行每行一个坐标(a,b):为各个草堆的坐标 输出拖拉机要回到原点(0,0)需要移动的草堆数量 Sampl ...

  6. 主页面与iframe页面之间的javascript函数的调用

    1:在主页面里调用iframe页里面的javascript函数 <script type="text/javascript"> var childWindow = $( ...

  7. JS数组 团里添加新成员(向数组增加一个新元素)只需使用下一个未用的索引,任何时刻可以不断向数组增加新元素。myarray[5]=88;

    团里添加新成员(向数组增加一个新元素) 上一节中,我们使用myarray变量存储了5个人的成绩,现在多出一个人的成绩,如何存储呢?  只需使用下一个未用的索引,任何时刻可以不断向数组增加新元素. my ...

  8. curl http_code 状态码

    1.只得到一个url的http_code的状态码 curl -I -m 10 -o /dev/null -s -w %{http_code} http://127.0.0.1/a.html 查询hea ...

  9. Excel skill: 如何替换换行符,以及如何把一格转换成多行/多列

    http://blog.sciencenet.cn/blog-508298-695290.html 增加一辅助列,用替换函数替换掉软回车.比如A列是数据,从A1开始,则插入B列,B1输入公式=REPL ...

  10. 日志服务Python消费组实战(二):实时分发数据

    场景目标 使用日志服务的Web-tracking.logtail(文件极简).syslog等收集上来的日志经常存在各种各样的格式,我们需要针对特定的日志(例如topic)进行一定的分发到特定的logs ...