koa洋葱模型

koa-compose:koa-compose则是将 koa/koa-router 各个中间件合并执行,结合 next() 形成一种串行机制,并且是支持异步,这样就形成了洋葱式模型

图解:一个洋葱来一刀


洋葱.png

有图有真相:等到next()函数执行完成之后,才会next()后面的代码,那么洋葱心就是最后一个执行完毕的中间件,每个next就是洋葱每一层的分界线

来个中间件试试水:

const Koa = require('koa');

const app = new Koa();

app.use(async (ctx, next) => {
    console.log(1);
    await next();
    console.log(1.1);
});

app.use(async (ctx, next) => {
    console.log(2);
    await next();
    console.log(2.2);
});

app.use(async (ctx, next) => {
    console.log(3);
    await next();
    console.log(3.3);
});

app.listen(3000, () => console.log('listening 3000'));

打印结果

// 1
// 2
// 3
// 3.3
// 2.2
// 1.1

开始剥洋葱~

  1. 接收函数,存入数组
  const app = {
    // 存放中间件的数组
    middlewares: [],
    // 存储方法,模拟使用中间件
    use(fn) {
        this.middlewares.push(fn)
    }
};
  1. compose: 开始串联每个函数,使用下标递增,并递归至最后一个函数结束
app.compose = function (middlewares) {
    return function () {
        // 从第一个函数开始
        dispath(0);

        function dispath(idx) {
            // 说明所有中间件都执行结束
            if (idx === app.middlewares.length) return;

            // 取出当前函数
            const fn = middlewares[idx];

            // 执行当前函数,传入next函数
            fn(function next() {
                // 并将下一个函数放入next中
                dispath(idx + 1);
            });
        }
    }
}
  1. 考虑支持异步,使用async/await
app.compose = function (middlewares) {
    return async function () {

        await dispath(0);

        async function dispath(idx) {
           
            if (idx === app.middlewares.length) return;

            const fn = middlewares[idx];

            await fn(function next() {
                
                dispath(idx + 1);
            });
        }
    }
}
  1. 测试一下
app.use(function (next) {
    console.log(1);
    next();
    console.log(1.1)
})

app.use(function (next) {
    console.log(2);
    next();
    console.log(2.2);

})

app.use(function (next) {
    console.log(3);
    next();
    console.log(3.3);
});


app.compose()(); 
// 执行顺序
// 1
// 2
// 3
// 3.3
// 2.2
// 1.1
  1. 为什么要是洋葱模型
    如果第一个中间件想得到后续中间件执行后的结果,那我们应该怎么办?
const Koa = require('koa');

const app = new Koa();

app.use(async (ctx, next) => {
    console.log(1);
    await next();
    // 取出lastVal
    console.log(1.1, ctx.lastVal);
});

app.use(async (ctx, next) => {
    console.log(2);
    await next();
    console.log(2.2);
});

app.use(async (ctx, next) => {
    console.log(3);
    await next();
    console.log(3.3);
    // 设置lastVal
    ctx.lastVal = 3.3;
});

app.listen(3000, () => console.log('listening 3000'));

// 1
// 2
// 3
// 3.3
// 2.2
// 1.1 3.3

通过挂载在ctx,我们可以取到最后中间件的结果: 这应该就是洋葱模型的真谛了吧~

参考:Koa-compose

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 分析 1、首先这是koa2最简单的入门例子,我将通过这个入门例子来演示koa2的洋葱模型 在这里面,app首先是调...
    隔壁老王的隔壁啊阅读 3,821评论 0 1
  • 本节将结合例子和源码对koa2的中间件机制做一介绍。 什么是中间件? 中间件的本质就是一种在特定场景下使用的函数,...
    空无一码阅读 1,465评论 0 2
  • 一、基本用法 1.1 架设 HTTP 服务 // demos/01.jsconst Koa = require('...
    majun00阅读 1,410评论 0 5
  • 前言 Koa 是运行在 Node.js 中的 web 服务框架,小而美。 Koa2 是 Koa 框架的最新版本,K...
    let_Scott阅读 5,810评论 2 28
  • koa介绍 Koa是由Express背后的团队创建的新流行的Web应用框架。它旨在成为Express的现代和极简主...
    初漾流影阅读 1,575评论 0 2