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

Add setting to yield next on OPTION request #30

Open
bearline opened this issue Nov 17, 2015 · 4 comments
Open

Add setting to yield next on OPTION request #30

bearline opened this issue Nov 17, 2015 · 4 comments

Comments

@bearline
Copy link

In most cases it makes sense to just return a 204 on an OPTIONS request, but the specification says that a body can be included with the response. Could we have some sort of setting that makes the module not capture the OPTIONS request?

To clarify, I need a setting that does something like below:

if (options.captureOptions === true && this.method === 'OPTIONS') {
    this.status = 204;
} else {
    yield next;
}
@andrenarchy
Copy link

+1

This also makes sense if koa-router is used with allowedMethods. This middleware sets the Allow: headers in response to an OPTIONS request. If koa-cors runs first, the Allow: headers from koa-router are missing in the response.

@fritx
Copy link

fritx commented May 14, 2017

+1 Related:

I was having a problem with OPTIONS in koa-router, if a xhr library sends an OPTIONS request, the koa-cors wouldn't work.

I solved it by using the following code:

// drop koa-cors

// api cors
apiRouter.use(async (ctx, next) => {
  ctx.set('Access-Control-Allow-Credentials', 'true')
  ctx.set('Access-Control-Allow-Origin', '*')
  await next()
})

// api options method
apiRouter.options('*', async (ctx, next) => {
  ctx.set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
  ctx.set('Access-Control-Allow-Origin', '*')
  ctx.status = 204
  await next()
})

@superman66
Copy link

I had met this problem. And I solved it by setting options foo koa-cors, add PATCH in methods:

const options = {
  origin: true,
  methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
};
app.use(cors(options))

@niftylettuce
Copy link
Collaborator

niftylettuce commented Jun 23, 2018

@fritx you are missing this from your example above:

  ctx.set(
    'Access-Control-Allow-Headers',
    ctx.get('Access-Control-Request-Headers')
  );

working code:

// Instead of using `@koa/cors` which conflicts with `koa-router`:
// https://github.com/evert0n/koa-cors/issues/30#issuecomment-301310688
routes.use(async (ctx, next) => {
  ctx.set('Access-Control-Allow-Credentials', 'true');
  ctx.set('Access-Control-Allow-Origin', '*');
  await next();
});
routes.options('*', async (ctx, next) => {
  ctx.set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
  ctx.set('Access-Control-Allow-Origin', '*');
  ctx.set(
    'Access-Control-Allow-Headers',
    ctx.get('Access-Control-Request-Headers')
  );
  ctx.status = 204;
  await next();
});

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

5 participants