首页 > 解决方案 > 在一个变量中调用多个中间件/参数 - deno Oak 框架

问题描述

我有这个代码

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";
});

怎么做?可以吗?

标签: typescriptdenooak

解决方案


Oak 带有compose中间件,允许您从多个中间件组成一个中间件

import { composeMiddleware as compose } from "https://deno.land/x/oak/mod.ts";

const onevar = compose([
    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";
});

推荐阅读