4

I have this code

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

it work's good , but i want to use keren and mantap in one variable called onevar

so the code gonna be like this :

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

how to do that? is it can?

1 Answers1

2

Oak comes with compose middleware that will allow you to compose a middleware from multiple middlewares

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";
});
Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98