Express.js错误:发送后无法设置标题
我最近一直在接受这个错误,尽pipe我一直在调查为什么我会收到这个错误,但我完全被难住了。 我从以前的研究中知道,这个错误通常与后续的res.send()调用相关联,其中第二个尝试在res.end()被执行后设置头文件。
这个过程是一系列的承诺,基本上从获取数据库中的一个对象开始,处理它,每个承诺使用前一个承诺的结果从数据库中获取其他数据并处理它,直到最终构build一个对象并将其发回给用户。
另一件事是,我们每秒超过2000个请求,并在一段时间后,我开始看到日志中的错误。 完整的错误信息是,然后是一堆Express Timeout错误:
Can't set headers after they are sent. Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (http.js:689:11) at ServerResponse.res.setHeader (/express/node_modules/connect/lib/patch.js:59:22) at ServerResponse.res.set.res.header (/node_modules/express/lib/response.js:522:10) at ServerResponse.res.jsonp (/node_modules/express/lib/response.js:236:8) at validator.validate.then.then.then.then.then.then.then.then.then.fail.res.send.code (<path-to-module>/module.js:131:29) at _fulfilled (/node_modules/q/q.js:798:54) at self.promiseDispatch.done (/node_modules/q/q.js:827:30) at Promise.promise.promiseDispatch (/node_modules/q/q.js:760:13) at /node_modules/q/q.js:574:44 at flush (/node_modules/q/q.js:108:17)
这里是代码:
app.get('/endpoint', function (req, res) { validator.validate(req.query, { // check query to see if valid }).then(function (valResult) { // if result passes validation get application id from database }).then(function (app) { // do a bunch of IO operations to build resulting object }).then }).then }).then }).then(function (result) { // This is where I am getting the error if (result.platform === 'mobile') { res.jsonp({code: 100, message: 'SUCCESS', data: result}); } else { res.send({code: 100, message: 'SUCCESS', data: result}); } }).fail(function (error) { if (error.stack) { console.log(error.message); console.log(error.stack); res.send({code: 500, message: 'FAIL_SYSTEM', data: error.message}); } else { res.send(error); } }); }
任何想法可能会发生什么? 是否有可能由于请求数量的原因而稍后执行某些承诺,并在响应已经发送后尝试发送?
承诺会这样工作,他将解释在nodejs validate.js中使用Q npm lib的小例子
var q = require('q'); function validate (query) { var deferred = q.difere(); ------validation logic ----- if (validation success) { deferred.resolve(validationResult); } else { deferred.reject(validationResult); } return deferred; }
在控制器中你可以写承诺
var validator = require('./validator.js'); app.get('/endpoint', function (req, res) { validator.validate(req.query) .then(function (valResult) { //same db query Model.find({},function(error, result){ if(!error) { return result ; } }); } ).then(function (result) { // This is where I am getting the error if (result.platform === 'mobile') { res.jsonp({code: 100, message: 'SUCCESS', data: result}); } else { res.send({code: 100, message: 'SUCCESS', data: result}); } }).fail(function (error) { if (error.stack) { console.log(error.message); console.log(error.stack); res.send({code: 500, message: 'FAIL_SYSTEM', data: error.message}); } else { res.send(error); } }); }).
- index.jade没有出现在本地主机上
- 亚马逊Alexa,alexa-app和中间件
- pipe道将Gulpstream文件的内容连接到服务器(connect,express或http)响应
- 在github页面上托pipe我的MEAN栈web应用程序
- RESTful Express Mongoose&Backbone – Backbone model.remove()不起作用
- “types错误:res.sendStatus不是一个函数”为什么我得到这个错误后有一段时间?
- TypeError:req.checkBody不是一个函数
- Express.js使用FormData对象获取API
- 服务器没有find任何匹配请求的URI节点的东西