【问题标题】:CORS not working in Express after config配置后 CORS 在 Express 中不起作用
【发布时间】:2018-03-05 13:36:33
【问题描述】:

我有两个基于 express 的服务器...

服务器 1:API 在端口 3010 上运行

服务器 2:UI 在端口 3000 上运行

在 app.js 文件 (autogen) 中的服务器 1 上,我有以下内容...

// 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 页面 POST 到该地址时,我在 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'); 和其他人,但似乎都没有工作

我错过了什么?

【问题讨论】:

  • 你也可以看看cors
  • @pzaenger 谢谢,但看看我的回答,看起来 express autogen 正在某个地方搞砸中间件
  • 啊,好吧。我知道了。你可能是对的 ;)

标签: express cors


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-03
    • 2020-06-23
    • 2017-04-19
    • 1970-01-01
    • 2022-01-15
    • 2015-07-15
    • 2018-11-05
    • 1970-01-01
    相关资源
    最近更新 更多