// 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...
koa 中的响应对象封装,基于 Node.js 的 http 请求的 response 做一些封装的属性和方法,挂载在ctx.response中 一个比较常用到的就是会有根据我们的ctx.body设置的值(会 delegate 到ctx.response.body中)帮我们去设置 response 的Content-Type的值,例如给ctx.body设置一个普通 js 对象的话,会将Content-Type设置为...
如果response.status未被设置, Koa 将会自动设置状态为200或204。 String Content-Type 默认为text/html或text/plain, 同时默认字符集是 utf-8。Content-Length 字段也是如此。 Buffer Content-Type 默认为application/octet-stream, 并且 Content-Length 字段也是如此。 Stream Content-Type 默认为application/octet-str...
app.use(async ctx => { ctx; // 这是 Context ctx.request; // 这是 koa Request ctx.response; // 这是 koa Response }); 为方便起见许多上下文的访问器和方法直接委托给它们的ctx.request或ctx.response,不然的话它们是相同的。 例如ctx.type和ctx.length委托给response对象,ctx.path和ctx.method委托...
response.headers 响应标头对象。别名是response.header。 response.socket 请求套接字。 response.status 获取响应状态。默认情况下,response.status设置为404而不是像 node 的res.statusCode那样默认为200。 response.status= 通过数字代码设置响应状态: 100 "continue" ...
app.use(async ctx => { ctx; // 这是 Context ctx.request; // 这是 koa Request ctx.response; // 这是 koa Response }); 为方便起见许多上下文的访问器和方法直接委托给它们的 ctx.request或 ctx.response ,不然的话它们是相同的。 例如 ctx.type 和 ctx.length 委托给 response 对象,ctx.path 和...
await next();//设置response的Content-Type:ctx.response.type = 'text/html';//设置response的内容:ctx.response.body = 'Hello, koa2!'; } 其中,参数ctx是由koa传入的封装了request和response的变量,我们可以通过它访问request和response,next是koa传入的将要处理的下一个异步函数。 上面的异步函数...
koa2框架设置响应和请求头 https://koa.bootcss.com/#response 请耐心翻到网页下端,可以看到 设置响应头: ctx.set('Content-Type', 'application/zip') 添加请求头: ctx.append('userName','111111');
app.use(async ctx => { ctx; // 这是 Context ctx.request; // 这是 koa Request ctx.response; // 这是 koa Response }); 为方便起见许多上下文的访问器和方法直接委托给它们的ctx.request或ctx.response,不然的话它们是相同的。 例如ctx.type和ctx.length委托给response对象,ctx.path和ctx.method委托...