【发布时间】:2015-03-15 09:07:39
【问题描述】:
我想为我的 node.js 服务器中的所有传入请求设置一个静态标头。 我目前的方法是为每个请求添加相同的 4 行标头(代码重复),我想解决这个问题,这是我要添加的标头的一个示例:
app.post('/postJob', function(req,res){
console.log("request method :" + req.method);
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// 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);
res.setHeader('Content-Type', 'application/json');
有人知道如何定义它吗?
【问题讨论】:
标签: javascript node.js http http-headers