Javascript 否';访问控制允许原点';react express docker应用程序

Javascript 否';访问控制允许原点';react express docker应用程序,javascript,node.js,reactjs,express,docker,Javascript,Node.js,Reactjs,Express,Docker,我使用docker运行前端react应用程序和node/express api。 react应用程序在localhost:3000上运行,api在localhost:9000上运行。它们都是功能齐全且工作正常的应用程序。但是,当我尝试从React应用程序向http://localhost:9000/api/whatever我遇到以下错误 XMLHttpRequest cannot load http://localhost:9000/api/schedule. No Access-Control

我使用docker运行前端react应用程序和node/express api。 react应用程序在
localhost:3000
上运行,api在
localhost:9000
上运行。它们都是功能齐全且工作正常的应用程序。但是,当我尝试从React应用程序向
http://localhost:9000/api/whatever
我遇到以下错误

XMLHttpRequest cannot load http://localhost:9000/api/schedule. No
Access-Control-Allow-Origin' header is present on the requested 
resource. Origin 'http://localhost:3000' is therefore not allowed access.`
这是我的express api的server.js文件:

const express = require('express');
const port = process.env.PORT || 9000;

const app = express();

require('./app/routes')(app);
app.use(function(req, res, next) {
    // Website you wish to allow to connect
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');

    // Request methods you wish to allow
    res.setHeader('Access-Control-Allow-Methods', 'GET');

    // Request headers you wish to allow
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

    // Set to true if you need the website to include cookies in the requests sent
    // to the API (e.g. in case you use sessions)
    res.setHeader('Access-Control-Allow-Credentials', true);

    // Pass to next layer of middleware
    next();
});
app.listen(port, () => {
    console.log('listening on port : ' + port);
})
不知道我错过了什么

Chrome网络选项卡:

Request URL:http://localhost:9000/api/schedule
Request Method:GET
Status Code:200 OK
Remote Address:[::1]:9000
Response Headers
view source
Connection:keep-alive
Content-Length:86
Content-Type:application/json; charset=utf-8
Date:Mon, 10 Apr 2017 04:40:06 GMT
ETag:W/"56-l2wi6AdD2GGTOMvRjRYO96YmR0w"
X-Powered-By:Express
Request Headers
view source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Host:localhost:9000
Origin:http://localhost:3000
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36

我以前通过以下配置可以做到这一点:

app.use((req, res, next) => {
  const origin = req.get('origin');

  // TODO Add origin validation
  res.header('Access-Control-Allow-Origin', origin);
  res.header('Access-Control-Allow-Credentials', true);
  res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization, Cache-Control, Pragma');

  // intercept OPTIONS method
  if (req.method === 'OPTIONS') {
    res.sendStatus(204);
  } else {
    next();
  }
});
正如您在我的案例中所看到的,我允许的方法不止是
GET
一个,还添加了其他允许的标题(例如在本例中我需要的
授权
一个),请注意,在您的案例中,您只指定了
X-request-With
内容类型
,但是,如果要验证原点,您可能还需要
原点
一个


我还拦截了
选项
请求,以避免在这种情况下发送额外的数据。

首先,您是否只发出GET请求?其次,您是否在请求中设置了任何“非标准”头?第三,发布浏览器代码会不会太难?是的,API现在只能获取。不设置任何非标准的内容。只需使用axios.get()。将使用网络选项卡更新。嗯,
响应标头中肯定没有CORS标头,对吧……我缺少什么?请尝试控制台req.headers并检查您得到的主机