Node.js Express应用处理启动错误 [英] Node.js Express app handle startup errors

查看:247
本文介绍了Node.js Express应用处理启动错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Node.js和Express中有应用程序。我需要为此编写测试。我在处理Express应用程序错误时遇到问题。我发现了这个如何捕获node.js / express服务器错误,例如EADDRINUSE?,但这对我不起作用,我不知道为什么。我想处理在expressApp.listen()执行时可能发生的错误(EADDRINUSE,EACCES等)。

  express = require('express')
监听器= express()

#对我不起作用
listener.on('uncaughtException',(err)->
#做某事


#也不起作用
listener.on( error,(err)->
#做某事


#这有效,但是它捕获了进程中的所有错误,我只想在侦听器
process.on('uncaughtException',(err)->
#做某事


listener.listen(80)#例如80错误

有什么想法吗?

解决方案

首先,expressJS不会抛出 uncaughtException 事件,进程可以,所以您的代码不起作用也就不足为奇了。



因此,请使用: process.on('uncaughtException',handler)



接下来,expressJS已经提供了错误处理的标准方法,该方法是使用为此目的提供的中间件功能,例如:

  app.configure(function(){
app.use(express.errorHandler({dumpExceptions:true,showStack:true}));
});

此函数向客户端返回一条错误消息,带有可选的堆栈跟踪,并记录在 connectJS errorHandler



(请注意,errorHandler实际上是connectJS的一部分,并且仅由expressJS公开。)



如果该行为现有的errorHandler提供的内容不足以满足您的需求,其来源位于 connectJS的 errorHandler 中间件,并且可以轻松修改以适应您的需求。



当然,与直接修改此函数相比,正确的方法是使用connectJS版本作为起点来创建自己的errorHandler,例如:

  var myErrorHandler = function(err,req,res,next){
...
// //注意,使用典型的中间件模式,我们在这里调用next() ,但
//由于此处理程序是提供者,即它终止了请求,因此我们
//不这样做。
};

并将其安装到expressJS中,如下所示:

  app.configure(function(){
app.use(myErrorHandler);
});

请参见只需连接,就可以解释connectJS的 filter provider 中间件的思想, 如何为Connect / Express编写中间件

您可能还会发现以下有用:





最后,有关以下内容的绝佳信息来源可以在它自己的测试。


I have app in Node.js and Express. I need to write tests for it. I have a problem with handling Express app errors. I found this How do I catch node.js/express server errors like EADDRINUSE?, but it doesn't work for me, I don't know why. I want to handle errors, which can occured while expressApp.listen() is executing (EADDRINUSE, EACCES etc.).

express = require('express')
listener = express()

#doesn't work for me
listener.on('uncaughtException', (err) ->
  #do something
)

#doesn't work too
listener.on("error", (err) ->
  #do something
)

#this works, but it caughts all errors in process, I want only in listener
process.on('uncaughtException', (err) ->
  #do something
)

listener.listen(80) #for example 80 to get error

Any ideas?

解决方案

First off, expressJS does not throw the uncaughtException event, process does, so it's no surprise your code doesn't work.

So use: process.on('uncaughtException',handler) instead.

Next, expressJS already provides a standard means of error handling which is to use the middleware function it provides for this purpose, as in:

app.configure(function(){
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

This function returns an error message to the client, with optional stacktrace, and is documented at connectJS errorHandler.

(Note that errorHandler is actually part of connectJS and is only exposed by expressJS.)

If the behavior the existing errorHandler provides is not sufficient for your needs, its source is located at connectJS's errorHandler middleware and can be easily modified to suit your needs.

Of course, rather than modifying this function directly, the "correct" way to do this is to create your own errorHandler, using the connectJS version as a starting point, as in:

var myErrorHandler = function(err, req, res, next){
    ...
    // note, using the typical middleware pattern, we'd call next() here, but 
    // since this handler is a "provider", i.e. it terminates the request, we 
    // do not.
};

And install it into expressJS as:

app.configure(function(){
    app.use(myErrorHandler);
});

See Just Connect it, Already for an explanation of connectJS's idea of filter and provider middleware and How To Write Middleware for Connect/Express for a well-written tutorial.

You might also find these useful:

Finally, an excellent source of information regarding testing expressJS can be found in its own tests.

这篇关于Node.js Express应用处理启动错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆