Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Cors for all routes #72

Open
bdcorps opened this issue Jun 30, 2022 · 7 comments
Open

Enable Cors for all routes #72

bdcorps opened this issue Jun 30, 2022 · 7 comments

Comments

@bdcorps
Copy link

bdcorps commented Jun 30, 2022

I see from the example that I can enable cors for that specific route - api/whoami but is there a way to apply this to all routes?

@gmariusze
Copy link

gmariusze commented Jul 26, 2022

By class inheritance or middleware (in nextjs you can place _middleware.ts in pages or api directory for instance). Your demand is not quite related to the library itself.

@nabtron
Copy link

nabtron commented Aug 31, 2022

@gmariusze your response makes the library less useful. The developer should explain how his library is useful, explaining how to implement it in projects.

@clearly-outsane
Copy link

I dont understand what was was wrong with the response, the author's response makes sense to me here

@Hades32
Copy link

Hades32 commented Feb 10, 2023

@yonycalsin The method signature for middlewares in NextJS _middleware.ts is export async function middleware(request: NextRequest) {. Therefore it's not really clear how to use this library, as we have to response arg

@steadylearner
Copy link

steadylearner commented Feb 11, 2023

You can read this blog post to use CORS for all routes with it.

You can see the repository you can use as an example here.

You can use this instead of the handler from Next js.

import NextCors from 'nextjs-cors';

function withNextCors(
  handler: NextApiHandler,
): NextApiHandler {
  return async function nextApiHandlerWrappedWithNextCors(req, res) {

    const methods = ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE'];
    await NextCors(req, res, {
      methods,
      origin: CORS_ALLOWED_ORGIN,
      optionsSuccessStatus: 200,
    });

    return handler(req, res);
  };
}

@jees47
Copy link

jees47 commented Jul 19, 2024

CORS_ALLOWED_ORGIN

whats inside CORS_ALLOWED_ORGIN ?

@AlexanderPershin
Copy link

CORS_ALLOWED_ORGIN

whats inside CORS_ALLOWED_ORGIN ?

I suppose it's an array of strings:

CORS_ALLOWED_ORGIN = ['http://localhost:5173',]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants