Chrome中的慢cors预检选项请求

Chrome中的慢cors预检选项请求

问题描述:

我正在努力解决一个怪异的问题,只在Chrome中发生。

I'm struggling with a freaky problem, that only occurs in Chrome.

我的角度SPA与另一个子域(api.domain.com )。为使此工作正常进行,我使用了 cors npm软件包。一切正常,除了OPTIONS预检在Chrome中的下载时间。

My angular SPA communicates with a node.js-Backend on a different subdomain (api.domain.com). To get this working, i use the cors npm package. Everything works fine, except the download-time of OPTIONS preflight in Chrome.

选项的屏幕截图比PUT慢

时间标签显示大多数时间都花在下载内容上,但我的回复中没有Content-Body。

此时间仅在Chrome中发生。 Firefox,IE,Edge和Opera不受影响。

This timing only occurs in Chrome. Firefox, IE, Edge and Opera are not affected.

我在docker容器中使用具有express和cors的node.js。前端是一个有角度的1 SPA。

I use node.js with express and cors inside docker containers. Frontend is an angular 1 SPA.

有人知道如何解决这个问题吗?

Has anyone an idea how to fix this?

If you are using express you can try this hack and write a middleware function with all your CORS stuff, check if the request is a preflight and just return 200. Hope this fixes it for you. But then again I don't use this dependency and its weird that it only occurs in chrome.

app.use(function(req, res, next) {    //CORS
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.setHeader("Access-Control-Allow-Headers", "Origin, x-access-token, X-Requested-With, Content-Type, Accept");
    if ('OPTIONS' == req.method) {
        res.send(200);
    }
    else {
        next();
    }
});