Koa 支持的响应体类型。
不同类型对应的处理方式如下:
string
直接写入响应流。
Content-Type 会被默认设置为 text/html
或者 text/plain
同时携带 charset=utf-8
。
Content-Length 会被设置为对应数值。
响应状态码如果没有指定则会被设置为 200,包括空字符串的情况。
Buffer
直接写入响应流。
Content-Type 会被默认设置为 application/octet-stream
。
Content-Length 会被设置为对应数值。
响应状态码如果没有指定则会被设置为 200。
Stream
Content-Type 会被默认设置为 application/octet-stream
。
当响应体被设置为 Stream 类型时,Koa 会自动添加一个 .onerror
监听器,用于捕获所有的 error 事件。
当请求连接断开(包括提前断开),Stream 会被销毁。
如果不需要这两个特性,则不要直接将响应体设置为 Stream 类型。
响应状态码如果没有指定则会被设置为 200。
Object 或者 Array
通过 JSON.stringify 进行序列化后写入响应流。
Content-Type 会被默认设置为 application/json; charset=utf-8
。
Content-Length 会被设置为对应数值。
响应状态码如果没有指定则会被设置为 200。
null
响应体无内容。
响应状态码如果没有指定则会被设置为 204。
Generated using TypeDoc
Copyright (c) Plank Root.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.