在开发过程中,我正在使用grunt-cont肋骨连接来服务我的应用程序,在升级到node.js 0.12之后,当我试图浏览我的应用程序时,我开始出现错误:
Error: "name" and "value" are required for setHeader().
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:333:11)
at ServerResponse.res.setHeader (/Users/abc/app/node_modules/grunt-contrib-connect/node_modules/connect/lib/patch.js:59:22)
at Object.module.exports.grunt.config.options.middleware.allowCors [as handle] (/Users/abc/app/grunt_tasks/connect.js:24:29)
at next (/Users/abc/app/node_modules/grunt-contrib-connect/node_modules/connect/lib/proto.js:190:15)
at Object.module.exports [as handle] (/Users/abc/app/node_modules/grunt-contrib-connect/node_modules/connect-livereload/index.js:84:5)
at next (/Users/abc/app/node_modules/grunt-contrib-connect/node_modules/connect/lib/proto.js:190:15)
at Function.app.handle (/Users/abc/app/node_modules/grunt-contrib-connect/node_modules/connect/lib/proto.js:198:3)
at Server.app (/Users/abc/app/node_modules/grunt-contrib-connect/node_modules/connect/lib/connect.js:65:37)
at Server.emit (events.js:110:17)
at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:491:12)除了升级节点之外,没有其他任何更改。我试着更新到最新版本的咕噜-控制-连接,但我仍然得到错误。
发布于 2015-03-19 18:37:20
从跟踪来看,该错误似乎发生在指定连接的中间件中,即函数allowCors中。通常指定一个中间件函数来设置响应上的CORS访问头。作为此功能的一部分,具有以下行是典型的:
res.setHeader('Access-Control-Allow-Origin', req.headers.origin);从节点0.12开始,它将对未定义req.headers.origin的请求抛出此错误。
在0.12中,进行了一项要求存在值的更改,而以前该值可能是未定义的。参见更改:https://github.com/joyent/node/commit/979d0ca874df0383311ca06f154f6965074196ee
在使用0.12时,当指定此中间件函数时,一个选项是在尝试在响应中设置访问头之前添加一个检查以查看是否定义了req.headers.origin:
if (req.headers.origin) {
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Access-Control-Allow-Origin', req.headers.origin);
res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version');
}https://stackoverflow.com/questions/29152462
复制相似问题