javascript Node.js Express 应用程序处理启动错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13322876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js Express app handle startup errors
提问by Piane_Ramso
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.).
我在 Node.js 和 Express 中有应用程序。我需要为它编写测试。我在处理 Express 应用程序错误时遇到问题。我发现了这个如何捕获像 EADDRINUSE 这样的 node.js/express 服务器错误?,但它对我不起作用,我不知道为什么。我想处理在 expressApp.listen() 执行时可能发生的错误(EADDRINUSE、EACCES 等)。
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?
有任何想法吗?
回答by Marius Tibeica
This should do the trick:
这应该可以解决问题:
listener.listen(80).on('error', function(err) { });
What listener.listen
actually does is create a HTTP server and call listen on it:
什么listener.listen
实际上做的是创建一个HTTP服务器和呼叫听就可以了:
app.listen = function(){
var server = http.createServer(this);
return server.listen.apply(server, arguments);
};
回答by Rob Raisch
First off, expressJS does not throw the uncaughtException
event, process does, so it's no surprise your code doesn't work.
首先,expressJS 不会抛出uncaughtException
事件,进程会抛出,因此您的代码不起作用也就不足为奇了。
So use: process.on('uncaughtException',handler)
instead.
所以使用:process.on('uncaughtException',handler)
代替。
Next, expressJS already provides a standard means of error handling which is to use the middleware function it provides for this purpose, as in:
接下来,expressJS 已经提供了一种标准的错误处理方式,即使用它为此目的提供的中间件功能,如下所示:
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.
此函数向客户端返回一条错误消息,带有可选的堆栈跟踪,并记录在connectJS errorHandler 中。
(Note that errorHandler is actually part of connectJS and is only exposed by expressJS.)
(注意 errorHandler 实际上是 connectJS 的一部分,并且只被 expressJS 暴露。)
If the behavior the existing errorHandler provides is not sufficient for your needs, its source is located at connectJS's errorHandler
middlewareand can be easily modified to suit your needs.
如果现有的 errorHandler 提供的行为不足以满足您的需求,其来源位于connectJS 的errorHandler
中间件,可以轻松修改以满足您的需求。
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:
当然,与其直接修改这个函数,“正确”的做法是创建自己的 errorHandler,使用 connectJS 版本作为起点,如下所示:
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:
并将其安装到 expressJS 中:
app.configure(function(){
app.use(myErrorHandler);
});
See Just Connect it, Alreadyfor an explanation of connectJS's idea of filter
and provider
middleware and How To Write Middleware for Connect/Express for a well-written tutorial.
请参阅Just Connect it, Already以了解 connectJSfilter
和provider
中间件的想法以及如何为 Connect/Express 编写中间件以获得编写良好的教程。
You might also find these useful:
这些对你也可能有用:
Finally, an excellent source of information regarding testing expressJS can be found in its own tests.
回答by Mohamed Allal
Mention: Marius Tibeicaanswer is complete and great, also david_pcomment is. As too is Rob Raisch answer (interesting to explore).
https://stackoverflow.com/a/27040451/7668448
https://stackoverflow.com/a/13326769/7668448
提及:Marius Tibeica 的回答是完整而伟大的,还有david_p评论。Rob Raisch 的回答也是如此(探索有趣)。
https://stackoverflow.com/a/27040451/7668448
https://stackoverflow.com/a/13326769/7668448
For those who will find this useful, here a function to implement busy port handling (if the port is busy, it will try with the next port, until it find a no busy port)
对于那些会发现这很有用的人,这里有一个实现繁忙端口处理的功能(如果端口繁忙,它将尝试使用下一个端口,直到找到一个不繁忙的端口)
app.portNumber = 4000;
function listen(port) {
app.portNumber = port;
app.listen(port, () => {
console.log("server is running on port :" + app.portNumber);
}).on('error', function (err) {
if(err.errno === 'EADDRINUSE') {
console.log(`----- Port ${port} is busy, trying with port ${port + 1} -----`);
listen(port + 1)
} else {
console.log(err);
}
});
}
listen(app.portNumber);
The function listen is recursively calling itself. In case of port busy error. Incrementing the port number each time.
函数 listen 递归调用自身。在端口繁忙错误的情况下。每次增加端口号。