没有错误被抛出未定义的variables在node.js与快递

我用express运行node.js。 我写了一个包含方法的节点模块,所以当你访问http://bla.com/module_name/method_name时 ,它将运行该方法。

该方法遵循典型的风格

exports.method_name(req, res, next); 

我的主要应用程序做这样的事情:

 app.all("*", resSetup, controller, render); 

而控制器是基于path调用方法的东西。

看起来如果在方法中有一个未定义的variables错误,express就会挂在那里而不会抛出任何错误。 控制台日志中也不会显示任何内容。 我可以把错误发生前后的控制台消息,之前会出现在日志中,之后将不会。

我可以把它包在一个try / catch并得到这个:

 [ReferenceError: blabla is not defined] 

但没有行号或任何东西。

我的猜测是,快递是以某种方式防止出现错误。 当我把错误放在直接在路由中的叫做“controller”的函数中时,它会正确显示错误。

这可能并不重要,但是这里是我正在处理的代码:

https://github.com/RobKohr/quick-site/blob/master/index.js

189行是方法调用发生的地方。

在上面的Ruairi的评论的基础上,当我使用'q'( https://github.com/kriskowal/q )时,我遇到了同样的问题,并且使用express的节点将会挂起,并且不会产生错误。

通过添加一个捕获的承诺'callback'链的末尾,我能够看到错误,并打印到控制台等。

代码最终看起来像:

 export function index(req, res) { //Create the 'promise' var request = req.body; var queryJobID = req.query.jobId; console.log('queryJobID: ' + queryJobID); var jobStatusPromsie = jobManager.job.getStatus(queryJobID); Q.all([jobStatusPromsie]) .then( function (result) { var responseData = {}; console.log('Job Status Response received'); if (result != null) { //Without the .catch below an error here will be 'silent' console.log('jobStatus result: ' + util.inspect(result, false, null)); responseData['status'] = 'OK'; responseData['progress'] = result.progress; res.json(responseData); } else { console.log('jobStatus Error'); responseData['status'] = 'Error'; } res.json(responseData); console.log('jobStatus Response data sent'); }, function (error) { console.log('Error while getting job status:', error); res.json("Error"); }) .catch(function(err) { //handle errors console.log('Promise error while getting job status:', err); }); } 
  1. Express主要依赖节点的asynchronous性。 看到像第30行一样的错误会让我感到毛骨悚然。 尝试重构你的代码只使用下一个(err)模式。

  2. 你应用挂起的原因是Express没有完成HTTP响应(例如:res.send())。 这意味着你已经在一个错误已经冒出调用堆栈但没有redirect到Express中间件pipe道的情况下破坏了pipe道。 尝试注册一些错误中间件 ,看看它是否被你的错误调用。