alephjs / aleph.js

The Full-stack Framework in Deno.
https://aleph.deno.dev/
MIT License
5.26k stars 168 forks source link

Set CORS Headers using Middleware #512

Closed Abiti-936 closed 2 years ago

Abiti-936 commented 2 years ago

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.

ije commented 2 years ago

in 1.0.0-beta.20 you can do this!