79 # koa 相应结果设置

简介: 79 # koa 相应结果设置

返回的类型可能是文件流,或者是对象的等方式,需要我们对 body 的类型进行判断在返回。

判断是否是 string 或者 buffer 、流、对象

if (typeof body === "string" || Buffer.isBuffer(body)) {
    res.end(body);
} else if (body instanceof Stream) {
    body.pipe(res);
} else if (typeof body === "object") {
    res.end(JSON.stringify(body));
}

我们在 application.js 添加结果类型的判断设置

const EventEmitter = require("events");
const http = require("http");
const context = require("./context");
const request = require("./request");
const response = require("./response");
const Stream = require("stream");
console.log("kaimo-koa---->");
class Application extends EventEmitter {
    constructor() {
        super();
        // 防止多个实例共享 context request response 需要进行拷贝
        this.context = Object.create(context);
        this.request = Object.create(request);
        this.response = Object.create(response);
        // 储存用户所有的 callback
        this.middlewares = [];
    }
    use(callback) {
        // 将用户传递的 callback 全部组合起来
        this.middlewares.push(callback);
    }
    // 创建一个上下文
    createContext(req, res) {
        // 每次请求都应该是一个全新的 context,需要拷贝
        let ctx = Object.create(this.context);
        // 上下文中有一个 request 对象,是自己封装的
        ctx.request = Object.create(this.request);
        // 上下文中还有一个 req 属性 指代的是原生的 req,自己封装的 request 对象上有 req 属性
        ctx.req = ctx.request.req = req;
        // 上下文中还有一个 response 对象,是自己封装的
        ctx.response = Object.create(this.response);
        // 上下文中还有一个 res 属性 指代的是原生的 res,自己封装的 response 对象上有 res 属性
        ctx.res = ctx.response.res = res;
        return ctx;
    }
    compose(ctx) {
        // 在数组中取出第一个,第一个执行后执行第二个
        const dispatch = (i) => {
            if (i === this.middlewares.length) return Promise.resolve();
            let middleware = this.middlewares[i];
            // 中间件如果不是 async 需要 Promise 包装一下,() => dispatch(i + 1) 就是 next
            return Promise.resolve(middleware(ctx, () => dispatch(i + 1)));
        };
        return dispatch(0);
    }
    async handleRequest(req, res) {
        const ctx = this.createContext(req, res);
        // 组合成一个线性结构依次执行,组合完返回一个大的 promise
        await this.compose(ctx);
        // 当组合后的 promise 完成后,拿到最终的结果响应回去
        let body = ctx.body;
        // 判断是否是 string 或者 buffer 、流、对象
        if (typeof body === "string" || Buffer.isBuffer(body)) {
            res.end(body);
        } else if (body instanceof Stream) {
            // 添加下载头
            res.setHeader("Content-Disposition", `attachment;filename=${encodeURIComponent("凯小默下载")}`);
            body.pipe(res);
        } else if (typeof body === "object") {
            res.end(JSON.stringify(body));
        }
    }
    listen(...args) {
        const server = http.createServer(this.handleRequest.bind(this));
        server.listen(...args);
    }
}
module.exports = Application;

编写 demo.js 测试对象类型

const Koa = require("./kaimo-koa");
const app = new Koa();
app.use(async (ctx, next) => {
    ctx.body = {
      kaimo: "313"
    };
});
app.listen(3000);

编写代码测试流,新建 test.txt

凯小默的博客
const fs = require("fs");
const Koa = require("./kaimo-koa");
const app = new Koa();
app.use(async (ctx, next) => {
    ctx.body = fs.createReadStream("./test.txt");
});
app.listen(3000);

访问 localhost:3000,就会直接下一个文件

目录
相关文章
|
5月前
Koa图片上传
koa2一般处理 post 请求使用的是 koa-bodyparser,图片上传使用的是 koa-multer。 这两个在一起没什么问题,但是 koa-multer 和 koa-route(不是 koa-router) 存在不兼容的问题。 故,建议在koa中全局引入
38 0
|
7月前
|
前端开发 中间件
74 # koa 的基本使用
74 # koa 的基本使用
19 0
|
7月前
|
前端开发 JavaScript 中间件
85 # koa 使用 koa-router 以及 koa-views 实现路由视图逻辑分离
85 # koa 使用 koa-router 以及 koa-views 实现路由视图逻辑分离
38 0
|
3月前
|
开发框架 JavaScript 前端开发
比较两个突出的node.js框架:koa和express
接上文讲述了 koa框架,这边文章比较一下这两个突出的node.js框架:koa和express
|
4月前
|
前端开发 JavaScript 中间件
koa开发实践2:为koa项目添加路由模块
koa开发实践2:为koa项目添加路由模块
57 0
|
4月前
|
消息中间件 中间件 API
什么是 Koa2?它与 Express 有什么区别?
什么是 Koa2?它与 Express 有什么区别?
105 0
|
网络架构
Koa 路由
Koa 路由
91 0
|
中间件 API
koa2如何允许跨域_koa2跨域模块koa2-cors
koa2如何允许跨域_koa2跨域模块koa2-cors
697 0
|
存储 JavaScript 中间件
重学Node.js及其框架(Express, Koa, egg.js) 之 Koa框架(下)
重学Node.js及其框架(Express, Koa, egg.js) 之 Koa框架
|
JSON JavaScript 中间件
重学Node.js及其框架(Express, Koa, egg.js) 之 Koa框架(上)
重学Node.js及其框架(Express, Koa, egg.js) 之 Koa框架