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

Handling CORS requests and missing OPTIONS handler in bao.ts #23

Open
lazydefi1 opened this issue Aug 9, 2023 · 4 comments
Open

Handling CORS requests and missing OPTIONS handler in bao.ts #23

lazydefi1 opened this issue Aug 9, 2023 · 4 comments

Comments

@lazydefi1
Copy link

How do you handle CORS requests?

CORS requests come from the browser through OPTIONS methods. It seems their handling is missing from bao.ts. Should I make a PR to add this?

@wedelgaard
Copy link

I would have assumed this would work, but apperently it doesn't:

   return ctx.sendJson(data, {
      headers: {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
      },
    });

@lazydefi1
Copy link
Author

lazydefi1 commented Sep 23, 2023

I would have assumed this would work, but apperently it doesn't:

   return ctx.sendJson(data, {
      headers: {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
      },
    });

I did something like this:

  app.after(async ctx => {
    if (ctx.res === null) { throw new Error('ctx.res is null'); }

    // CORS
    ctx.res.headers.set('Access-Control-Allow-Origin', '*');
    ctx.res.headers.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    ctx.res.headers.set('Access-Control-Allow-Headers', 'Content-Type');

    return ctx;
  });

Also, correct me if I'm wrong but the OPTIONS handling in bao is missing, I added it like this:
f566896

@erikvullings
Copy link

@lazydefi1 Could you perhaps create a PR for this update. Without the OPTIONS handling, I cannot really implement CORS, as my app sends a preflight OPTIONS request to check for CORS headers that I cannot handle. I hope it will be accepted, though, since the last code change is 3 months old, so that is not very promising...

I've implemented a work around using the any handler, but that is ugly and not very easy to understand.

@mattreid1
Copy link
Owner

I've created PR #28 to support this. Please take a look and let me know what you think as I haven't actually tested it yet - if anyone wants to write some test cases that would be much appreciated!

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

4 participants