alephjs / aleph.js

The Full-stack Framework in Deno.

Home Page:https://alephjs.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Set CORS Headers using Middleware

Abiti-936 opened this issue · comments

I am trying to build a Rest API using Alephjs & Alephjs file based routing is Great, but When I am trying to set CORS headers for all routes, I can't do it using a middleware.

I am try to do it like these:

import { serve } from "aleph/server";
import routes from "./routes/_export.ts";
import cors from "https://deno.land/x/edge_cors@0.2.1/src/cors.ts";

serve({
  router: { routes },
  middlewares: [
    {
      name: "cors-middleware",
      async fetch(request, { next }) {
        // next() will invoke a respective route function 
        const response = await next();
        return cors(request, response);
      },
    },
  ],
});

I think there should be a way of getting a route response within a middleware?

Thanks.

commented

in 1.0.0-beta.20 you can do this!