Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call multiple middleware / arguments in one variable - deno oak framework

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 Answers

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";
});
like image 154
Marcos Casagrande Avatar answered Oct 28 '25 03:10

Marcos Casagrande