Giter VIP home page Giter VIP logo

Comments (4)

niftylettuce avatar niftylettuce commented on July 18, 2024 4

@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();
});

from koa-cors.

andrenarchy avatar andrenarchy commented on July 18, 2024 1

+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.

from koa-cors.

fritx avatar fritx commented on July 18, 2024 1

+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()
})

from koa-cors.

superman66 avatar superman66 commented on July 18, 2024 1

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))

from koa-cors.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.