我是靠谱客的博主 明亮睫毛膏,这篇文章主要介绍【Koa2】洋葱模型的理解,现在分享给大家,希望可以做个参考。

实践出真知之Koa洋葱模型

在这里插入图片描述
在这里插入图片描述

核心代码演示:

const Koa = require('koa')
const app = new Koa()

//middleware one
app.use(async(ctx,next) => {
    console.log("中间件一开始");
    await next() //调用下一个中间件
    console.log("中间件一结束");
})

//middleware two
app.use(async (ctx,next) => {
    const startTime = new Date().getTime();
    console.log("中间件二开始");
    await next(); //处理下一个中间件
    console.log("中间件二结束");
    const spendMs = new Date().getTime() - startTime
})

//middleware three
app.use(async (ctx,next) => {
    console.log("++++");
    await next()
    ctx.response.type = "text/html"
    ctx.response.body = "Koa洋葱模型"
    console.log("----");
})

app.listen(3000);

执行结果:

在这里插入图片描述
从执行结果中可以看出,从第一个中间件开始,最后从第一个中间件结束,就像针穿过洋葱一样,从最外层进去,然后从最外层出来一样。

最后

以上就是明亮睫毛膏最近收集整理的关于【Koa2】洋葱模型的理解的全部内容,更多相关【Koa2】洋葱模型内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部