允许NodeJS和Express处理Cache-Control头

我有一个nodeJS服务器负责通过REST服务一些资源。

最近API的一个消费者开始在头上添加这个: “Cache-Control”:“no-cache”

节点使用a: Request标头字段拒绝预检选项Cache-Control不允许出现Access-Control-Allow-Headers错误。

我没有这个问题。 但是我不确定我在哪里可以在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 (eg 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(); }); 

我在这里回答。

caching控制需要列在Access-Control-Allow-Header(令人惊讶的权利,正是错误告诉我的)。 不只是作为一个特定的标题。

所以,在上面的代码中我们需要replace

res.setHeader('Access-Control-Allow-Headers', 'my-header,X-Requested-With,content-type,Authorization');

  res.setHeader('Access-Control-Allow-Headers', 'my-header,X-Requested-With,content-type,Authorization,cache-control');