【发布时间】:2015-12-29 12:12:56
【问题描述】:
我在 nodeJS 中有一个服务器,负责通过 REST 提供一些资源。
最近,API 的一位使用者开始在标题中添加:"Cache-Control": "no-cache"
节点拒绝预检选项: Access-Control-Allow-Headers 不允许请求标头字段 Cache-Control 错误。
我对此没有意见。但我不确定在 Express 中哪里可以允许这样做。
这就是我的 express 中间件的样子
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'my-header,X-Requested-With,content-type,Authorization');
//res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
// 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);
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
res.setHeader("Pragma", "no-cache"); // HTTP 1.0.
res.setHeader("Expires", "0"); // Proxies.
// Pass to next layer of middleware
next();
});
【问题讨论】: