// koa/lib/response.jsset body(val) { const original = this._body; this._body = val; if (this.res.headersSent) return; // no content if (null == val) { if (!statuses.empty[this.status]) this.status = 204; this.remove('Content-Type'); this.remove('Content-Length'); this....
response.json 本质上也是调用 response.send 方法,所以只需要分析一下response.send 的源码即可。res.send 中通过判断chunk (body) 的类型,以及Content-Type 的值,来动态设置 Content-Type类型,使得浏览器知道响应的内容是什么类型数据。Express请求响应Content-Type类型常见有:res.type('.html');res.type('html...
Content-Type 默认为application/json. 这包括普通的对象{ foo: 'bar' }和数组['foo', 'bar']。 response.get(field) 不区分大小写获取响应头字段值field。 const etag = ctx.response.get('ETag'); response.has(field) 如果当前在响应头中设置了由名称标识的消息头,则返回true. 消息头名称匹配不区分大小...
比如 response.type = 'html' 将默认是 "utf-8". 如果你想覆盖 charset, 使用 ctx.set('Content-Type', 'text/html') 将响应头字段设置为直接值。
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"
koa2框架设置响应和请求头 https://koa.bootcss.com/#response 请耐心翻到网页下端,可以看到 设置响应头: ctx.set('Content-Type', 'application/zip') 添加请求头: ctx.append('userName','111111');
response.set(fields) 使用对象同时设置 response header 中多个字段的值。 ctx.set({ 'Etag': '1234', 'Last-Modified': date }); 1. 2. 3. 4. response.remove(field) 移除response header 中字段filed。 response.type 获取responseContent-Type,不包含像"charset"这样的参数。
await next();//设置response的Content-Type:ctx.response.type = 'text/html';//设置response的内容:ctx.response.body = 'Hello, koa2!'; } 其中,参数ctx是由koa传入的封装了request和response的变量,我们可以通过它访问request和response,next是koa传入的将要处理的下一个异步函数。 上面的异步函数...
response.message 获取响应的状态消息. 默认情况下,response.message与response.status关联. response.message= 将响应的状态消息设置为给定值。 response.length= 将响应的 Content-Length 设置为给定值。 response.length 以数字返回响应的 Content-Length,或者从ctx.body推导出来,或者undefined。
koa 中的响应对象封装,基于 Node.js 的 http 请求的 response 做一些封装的属性和方法,挂载在ctx.response中 一个比较常用到的就是会有根据我们的ctx.body设置的值(会 delegate 到ctx.response.body中)帮我们去设置 response 的Content-Type的值,例如给ctx.body设置一个普通 js 对象的话,会将Content-Type设置为...