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 为如下值:
stringwrittenBufferwrittenStreampipedObjectjson-stringifiednullno 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)。
KOA 学习(四)的更多相关文章
- TweenMax动画库学习(四)
目录 TweenMax动画库学习(一) TweenMax动画库学习(二) TweenMax动画库学习(三) Tw ...
- Koa 学习笔记
开始 就像官网上说的,一切框架都从一个"Hello World"开始,首先我们新建一个 package.json,内容尽量简单: { "name": " ...
- koa 学习资料
koa 学习资料 学习资料 地址 koa 中文版 https://koa.bootcss.com/
- SVG 学习<四> 基础API
目录 SVG 学习<一>基础图形及线段 SVG 学习<二>进阶 SVG世界,视野,视窗 stroke属性 svg分组 SVG 学习<三>渐变 SVG 学习<四 ...
- Android JNI学习(四)——JNI的常用方法的中文API
本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...
- SCARA——OpenGL入门学习四(颜色)
OpenGL入门学习[四] 本次学习的是颜色的选择.终于要走出黑白的世界了~~ OpenGL支持两种颜色模式:一种是RGBA,一种是颜色索引模式. 无论哪种颜色模式,计算机都必须为每一个像素保存一些数 ...
- ZigBee学习四 无线+UART通信
ZigBee学习四 无线+UART通信 1) 协调器编程 修改coordinator.c文件 byte GenericApp_TransID; // This is the unique messag ...
- (转)SpringMVC学习(四)——Spring、MyBatis和SpringMVC的整合
http://blog.csdn.net/yerenyuan_pku/article/details/72231763 之前我整合了Spring和MyBatis这两个框架,不会的可以看我的文章MyBa ...
- Spring Boot 项目学习 (四) Spring Boot整合Swagger2自动生成API文档
0 引言 在做服务端开发的时候,难免会涉及到API 接口文档的编写,可以经历过手写API 文档的过程,就会发现,一个自动生成API文档可以提高多少的效率. 以下列举几个手写API 文档的痛点: 文档需 ...
- Expression Blend学习四控件
原文:Expression Blend学习四控件 Expression Blend制作自定义按钮 1.从Blend工具箱中添加一个Button,按住shift,将尺寸调整为125*125; 2.右键点 ...
随机推荐
- 2019牛客国庆集训派对day5
2019牛客国庆集训派对day5 I.Strange Prime 题意 \(P=1e10+19\),求\(\sum x[i] mod P = 0\)的方案数,其中\(0 \leq x[i] < ...
- POJ - 2774~POJ - 3415 后缀数组求解公共字串问题
POJ - 2774: 题意: 求解A,B串的最长公共字串 (摘自罗穗骞的国家集训队论文): 算法分析: 字符串的任何一个子串都是这个字符串的某个后缀的前缀. 求 A 和 B 的最长 公共子串等价于求 ...
- uoj349 即时战略
题意:这是一道交互题.交互库中有一棵树.一开始只有1节点已知.需要在T次询问内使得n个节点都已知.一次询问explore(x,y),返回从x到y路径上第一个点,并将返回点标记为已知. 数据有区分. 标 ...
- leetcode-129-求根到叶子结点数字之和
题目描述: 第一次提交: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self. ...
- 一台电脑同时添加git和bitbucket两个网站的ssh key
添加第一个ssh key 就不多说了,不懂的可以自己查资料 ssh-keygen -t rsa -C 'email_1@email.com' 然后一路enter就好了 假设已经添加好了git的ssh ...
- vue + element-ui实现简洁的导入导出功能
1.安装ElementUI模块 cnpm install element-ui -S 2.在main.js中引入 import ElementUI from 'element-ui' import ' ...
- 北京服务业占GDP比重达81.7%
北京服务业占GDP比重达81.7% 2017-05-17 19:46:00 来源: 中国新闻网(北京)举报 0 易信 微信 QQ空间 微博 更多 (原标题:北京服务业占GDP比重达81.7%) ...
- iOS之CGcontext.h方法和属性简介
/* CoreGraphics - CGContext.h Copyright (c) 2000-2012 Apple Inc. All rights reserved. */ #ifndef CGC ...
- PAT甲级——A1106 Lowest Price in Supply Chain
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- 左神算法进阶班8_1数组中累加和小于等于aim的最长子数组
[题目] 给定一个数组arr,全是正数:一个整数aim,求累加和小于等于aim的,最长子数组,要求额外空间复杂度O(1),时间复杂度O(N) [题解] 使用窗口: 双指针,当sum <= aim ...