如何在KeystoneJSv4中设置CORS

时间:2018-09-12 12:22:52

标签: keystonejs

我有一个在KeystoneJS以外的服务器上运行的VueJS应用程序。 我想将请求从VueJS应用程序发送到Keystone。

如何在版本4中激活CORS?

2 个答案:

答案 0 :(得分:1)

在索引文件中,首先设置您的CORS配置,例如来源,标头和方法:

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

然后,您需要在路由文件中为路由应用配置。

对所有路线应用CORS:

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

将CORS应用于特定的/about路由:

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

答案 1 :(得分:0)

只需将以下代码放入 routes / index.js

app.use('*', function (req, res, next) {
        res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-XSRF-TOKEN');
        res.header('Access-Control-Allow-Origin', '*');
        res.header('Access-Control-Allow-Method', 'GET,POST,PUT,DELETE');
        res.header('Access-Control-Allow-Credentials', true);
        next();
    });
    app.options('*', function (req, res) {
        res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-XSRF-TOKEN');
        res.header('Access-Control-Allow-Origin', '*');
        res.header('Access-Control-Allow-Method', 'GET,POST,PUT,DELETE');
        res.header('Access-Control-Allow-Credentials', true);
        res.sendStatus(200);
    });