This is my code (It's coffeescript and uses colors but that's not relevant)
这是我的代码(它是coffeescript并使用颜色,但这不相关)
# If this module is executed
if !module.parent
# Start server
try
hons_server.listen config.port
console.log 'Listening to port ' + config.port
catch err
console.error "Couldn't start server: #{String(err)}".red
hons_server
is an express.js server. I'm having a hard time understanding why errors thrown as a result of hons_server.listen()
aren't caught by the try/catch. When I run my server twice I get the output:
hons_server是一个express.js服务器。我很难理解为什么由于hons_server.listen()抛出的错误没有被try / catch捕获。当我运行我的服务器两次时,我得到输出:
$ coffee src/server.coffee Listening to port 9090 node.js:201 throw e; // process.nextTick error, or 'error' event on first tick ^ Error: listen EADDRINUSE at errnoException (net.js:632:11) at Array.0 (net.js:733:26) at EventEmitter._tickCallback (node.js:192:40)
I'd like to know why the thrown error isn't caught, and how/where I can catch the EADDRINUSE error.
我想知道为什么没有捕获抛出的错误,以及我如何/在哪里可以捕获EADDRINUSE错误。
2 个解决方案
#1
19
Listen for the error
event on the server isntance
侦听服务器上的错误事件
hons_server.on 'error', (err) ->
console.log 'there was an error:', err.message
#2
23
The accepted solution did not work for me on nodejs 0.8.22 and express 3.1.0.
在nodejs 0.8.22和express 3.1.0上,我接受的解决方案对我不起作用。
This did the trick:
这样就可以了:
process.on('uncaughtException', function(err) {
if(err.errno === 'EADDRINUSE')
console.log(...);
else
console.log(err);
process.exit(1);
});
Also see Node.js Express app handle startup errors
另请参阅Node.js Express应用程序处理启动错误
#1
19
Listen for the error
event on the server isntance
侦听服务器上的错误事件
hons_server.on 'error', (err) ->
console.log 'there was an error:', err.message
#2
23
The accepted solution did not work for me on nodejs 0.8.22 and express 3.1.0.
在nodejs 0.8.22和express 3.1.0上,我接受的解决方案对我不起作用。
This did the trick:
这样就可以了:
process.on('uncaughtException', function(err) {
if(err.errno === 'EADDRINUSE')
console.log(...);
else
console.log(err);
process.exit(1);
});
Also see Node.js Express app handle startup errors
另请参阅Node.js Express应用程序处理启动错误