koa-compose 合并中间件

下载

  •  npm install koa-compose 

使用

  • 复制代码
    const Koa = require('koa')
    const compose = require('koa-compose')
    const app = new Koa()
    
    const a1 = (ctx, next) => {
      console.log('a1')
      next()
    }
    
    const a2 = (ctx, next) => {
      console.log('a2')
      next()
    }
    
    const a3 = (ctx, next) => {
      console.log('a3')
      next()
    }
    
    // app.use(a1)
    // app.use(a2)
    // app.use(a3)
    app.use(compose([a1, a2, a3]))
    
    app.use(async (ctx, next) => {
      ctx.body = 'hello koa'
    })
    
    app.listen(4001)
    复制代码

    将多个中间件传递到 compose 数组中,实现挂载数组中所有中间件

posted @   霸哥yyds  阅读(79)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示