KOA 学习(四)

响应(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‘: ‘1234‘,
  ‘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 = 301;
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)。

时间: 2024-08-05 14:44:19

KOA 学习(四)的相关文章

nodejs学习四 Node.js NPM

什么是NPM? 不知道大家注意没有,windows平台下的Node.js安装包大小才区区4M多,真可以用短小精悍来形容它,作为一种编程语言,像java一个SDK 就几十M,为什么node.js的运行环境这么小呢?这其中的微妙之处在于,它拥有一个庞大的第三方软件库. 在Node本身提供的包(原生)中没有我们要实现的功能模块的时候,我们可以去寻找下是否已经有人实现了这种功能.毕竟重复造轮子这种事情,很多人都不想干. 去哪里寻找我们想要的包呢?如果你还不知道报的名字,你可以去https://www.n

ZigBee学习四 无线+UART通信

ZigBee学习四 无线+UART通信 1) 协调器编程 修改coordinator.c文件 byte GenericApp_TransID; // This is the unique message ID (counter) afAddrType_t GenericApp_DstAddr; //unsigned char uartbuf[128];/********************************************************************** L

Spark学习四:网站日志分析案例

Spark学习四:网站日志分析案例 标签(空格分隔): Spark Spark学习四网站日志分析案例 一创建maven工程 二创建模板 三日志分析案例 一,创建maven工程 1,执行maven命令创建工程 mvn archetype:generate -DarchetypeGroupId=org.scala-tools.archetypes -DarchetypeArtifactId=scala-archetype-simple -DremoteRepositories=http://scal

Beaglebone Back学习四(GPIO实验)

GPIO Beaglebone Back开发板引出了92个引脚,其中只有65个GPIO口可通过配置使用,由于引脚具有"复用"的特性,大约每个引脚有8种工作模式(Beagle System Reference Manual),默认情况下,设为Mode7.由于P8扩展部分的引脚功能相对简单,复用不多,故项目功能复杂时,最好选择P8上的GPIO口. 对GPIO口的操作,主要有三个步骤 1 选择GPIO口根据以下两表,确定使用那个GPIO口,该表也可以在BBB_SRM文件中找到. (1)拉电流

Silverlight学习(四) domainservice动态多条件查询

上次讲了silverlight+MVVN+EF的简单框架,能够实现简单的数据CURD,但是多条件动态的查询一直没有实现.在网上查阅了很多资料,发现自己走了很多误区,代码很难调试正确. 这次的查询是基于上次的查询,只是增加了一个查询条件,动态多条件的查询的重点是获取查询的语言. 1 private string GetSql() 2 { 3 string query = "1=1"; 4 if (!string.IsNullOrEmpty(searchText.name)) 5 { 6

Gradle学习(四) web工程构建

Gradle为应用开发提供了两个相关的插件:war plugin以及jetty plugin war plugin继承了java plugin为你的工程构建war包,jetty pugin继承了war plugin可以让的工程构建在嵌入式容器jetty中 构建War文件 首先在你的build.gradle中添加如下一行 apply plugin: 'war' 由于war plugin继承了java plugin,所有java plugin也会被默认的加入配置文件中 运行gradle build命

TweenMax动画库学习(四)

目录            TweenMax动画库学习(一)            TweenMax动画库学习(二)            TweenMax动画库学习(三)            TweenMax动画库学习(四)            TweenMax动画库学习(五)  

Android学习四、Android中的Adapter

一.Adapter的介绍 An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set. 一个Adapter是Ad

Cmdlet开发与学习(四)

前面讲完了一个基本的cmdlet的开发流程,下面将陆续地完善cmdlet的功能细节以及补充.       支持ShouldProcess       有的cmdlet操作有很大的破坏性,因此有必要再命令执行前提醒用户注意一些可能的执行后果,也就是说,存在与用户的交互.这个时候,通过使用ShouldProcess,可以达到预期的效果. 1 [Cmdlet(VerbsCommon.Get,"SQLServerBackupFile", 2 SupportsShouldProcess = tr

JBPM学习(四):执行流程实例

1.创建表空间 create tablespace TEST logging datafile 'e:\app\administrator\oradata\orcl\TEST.dbf' size 100M autoextend on next 100M maxsize 4096M extent management local; 2.创建用户并指定表空间 create use testuser identified by password default tablespace TEST temp