2017-09-24 37 views
0

後我有兩個明確的服務器...CORS在快遞不能正常工作配置

服務器1:API端口運行3010

服務器2:UI 3000端口

運行在服務器1在app.js文件(AUTOGEN)我有以下...

// Allow requests from the ui 
app.use(function (req, res, next) { 
    // TODO: Make specific 
    res.header('Access-Control-Allow-Origin', '*'); 
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); 
    res.header('Access-Control-Allow-Headers', 'Content-Type'); 

    next(); 
}); 

但是,當我嘗試從服務器2頁我得到在Chrome控制檯下面的帖子到該地址....

Failed to load http://localhost:3010/search: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

我也試過res.header('Access-Control-Allow-Origin', 'http://localhost:3000');和其他人,但沒有似乎工作

我缺少什麼?

+0

你也可以看看[cors](https://www.npmjs.com/package/cors)。 – pzaenger

+0

@pzaenger謝謝你,但看到我的答案,它看起來像快遞autogen擰中間件下一個電話 – Jackie

+0

啊,好吧。我懂了。你可能是對的 ;) – pzaenger

回答

0

我移動app.use提前其他生成的app.uses,它似乎工作。仍然不確定爲什麼,猜測autogen中的中間件項目沒有正確調用。