提问者:小点点

在一个变量javascript函数中调用多个中间件/参数


我有这个密码

const keren = async (ctx: Context, next: any) => {
  console.log('keren');

  await next();
}

const mantap = async (ctx: Context, next: any) => {
  console.log('mantap');

  await next();
}

router.get('/owkowkkow',keren,mantap,(ctx: Context) => {
  ctx.response.body = "wkwkwkw";
});

工作不错,但我想在一个名为onevar变量中使用keren和mantap

所以代码应该是这样的:

router.get('/owkowkkow',onevar,(ctx: Context) => {
  ctx.response.body = "wkwkwkw";
});

怎么做? 是罐头吗?


共1个答案

匿名用户

在Express和其他框架上,您可以传递一组中间件:

const onevar = [
    async (ctx: Context, next: any) => {
      console.log('keren');

      await next();
    },

    async (ctx: Context, next: any) => {
      console.log('mantap');

      await next();
    }
]

router.get('/owkowkkow',onevar,(ctx: Context) => {
  ctx.response.body = "wkwkwkw";
});