CORS在快递不能正常工作配置

问题描述:

后我有两个明确的服务器...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

我移动app.use提前其他生成的app.uses,它似乎工作。仍然不确定为什么,猜测autogen中的中间件项目没有正确调用。