koa洋葱模型

作者: 贝程学院_前端 | 来源:发表于2019-06-25 22:13 被阅读1次

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

相关文章

  • 【Node】深入浅出 Koa 的洋葱模型

    本文将讲解 koa 的洋葱模型,我们为什么要使用洋葱模型,以及它的原理实现。掌握洋葱模型对于理解 koa 至关重要...

  • koa洋葱模型

    koa-compose:koa-compose则是将 koa/koa-router 各个中间件合并执行,结合 ne...

  • koa洋葱模型

    前几天面试node,面试官问了koa的中间件是如何实现的,我一想,卧槽,这特么不是我很熟悉的么,然后就哇啦啦啦的一...

  • koa 洋葱模型

    分析 1、首先这是koa2最简单的入门例子,我将通过这个入门例子来演示koa2的洋葱模型 在这里面,app首先是调...

  • 【源码学习----koa】koa中间件核心(koa-compos

    最近经常使用koa进行服务端开发,迷恋上了koa的洋葱模型,觉得这玩意太好用了。而且koa是以精简为主,没有很多集...

  • koa 中间件机制以及异常捕获

    koa 中间件机制以及异常捕获 koa 中间件机制解析 koa 的请求处理是典型的洋葱模型,下面是官方的配图,而这...

  • koa洋葱模型原理

    首先,关于 洋葱模型 自上而下-》自下而上 回溯机制并不是 koa 自己的特性,而是 async/await 自己...

  • koa全攻略

    1.什么是洋葱模型 简单介绍 用一句话来说,koa,express框架的中间件的执行顺序,可以比喻成洋葱模型。 我...

  • jk node笔记(2)

    express 中间件在没有异步的情况下,符合洋葱模型,一旦有了异步,就会打破洋葱模型。koa 中使用异步函数写中...

  • koa

    koa的处理流程 koa的中间件 洋葱模型实现 通过await next();进入下一个中间件 next是comp...

网友评论

    本文标题:koa洋葱模型

    本文链接:https://www.haomeiwen.com/subject/nxjfcctx.html