Search code examples
keystonejs

How to set CORS in KeystoneJSv4


I have a VueJS application running on a server other than KeystoneJS. I would like to send requests from the VueJS applications to Keystone.

How do I activate CORS in version 4?


Solution

  • In your index file, first set your CORS config, like origin, header and methods:

    // example for allow all
    keystone.set('cors allow origin', true);
    keystone.get('cors allow methods', true)
    keystone.get('cors allow methods', headers)
    

    Then you need to apply the config for your routes in your route files.

    Apply CORS to all routes:

    app.all('/*', keystone.middleware.cors);
    

    Apply CORS to a specific /about route:

    app.all('/about', keystone.middleware.cors);