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

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

问题描述

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

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

有什么想法吗?

推荐答案

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

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

所以改用:process.on('uncaughtException',handler).

接下来,expressJS 已经提供了一种标准的错误处理方法,即使用它为此目的提供的中间件函数,如下所示:

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 }));
});

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

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

(注意errorHandler其实是connectJS的一部分,只被expressJS暴露出来.)

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

如果现有 errorHandler 提供的行为不足以满足您的需求,则其来源位于 connectJS 的 errorHandler 中间件,可以轻松修改以满足您的需求.

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.

当然,与其直接修改这个函数,正确"的做法是创建自己的errorHandler,以connectJS版本为起点,如:

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.
};

并将其安装到 expressJS 中:

And install it into expressJS as:

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

请参阅 Just Connect it, Already 了解 connectJS 的 filterprovider 中间件和 How To Write Middleware for Connect/Express 以获得编写良好的教程.

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:

从 Node.JS 中的未捕获异常中恢复

最后,可以在 它自己的测试中找到有关测试 expressJS 的极好信息来源.

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

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