【发布时间】:2017-08-15 09:03:40
【问题描述】:
我正在尝试使用 express.js CORS package 将 CORS 请求的来源限制为每个路由的一个特定域,如下所示:
const express = require('express');
const cors = require('cors');
const port = process.env.PORT || 3000;
let app = express();
app.get('/', cors({origin: 'http://example.com'}), (req, res, next) => {
res.sendStatus(200);
});
app.post('/', cors({origin: 'http://whatever.com'}) (req, res, next) => {
res.sendStatus(200);
});
app.listen(port, () => {
console.log(`Started on port ${port}`);
});
但是,这似乎没有任何效果,因为我可以在任何域中使用 GET 和 POST。然后我尝试使用以下方法将所有路线限制为一个单一来源,但遇到了相同的结果:
app.use(cors({origin: 'http://example.com'}));
我在本地主机上的开发环境和 Heroku 上的生产环境中都遇到了这种情况。知道我错过了什么吗?
【问题讨论】:
-
stackoverflow.com/questions/11001817/… 允许访问控制应设置为您想接受我认为的请求的站点
-
@Rico 例如,我应该怎么做才能只允许
POST请求来自某个域? -
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
-
仍然允许任何来源发布。我并不像起源那样关心方法
-
res.header('Access-Control-Allow-Origin', 'http:/ricoGotSwag.com'); res.header('Access-Control-Allow-Methods', 'POST');
标签: node.js express heroku http-headers cors