How do I catch node.js/express server errors like EADDRINUSE?

后端 未结 2 1447
既然无缘
既然无缘 2020-12-16 10:03

This is my code.

if !module.parent
    try
        hons_server.listen config.port
        console.log \'Listening to port \' + config.port
    catch err
             


        
相关标签:
2条回答
  • 2020-12-16 10:14

    The accepted solution did not work for me on nodejs 0.8.22 and 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

    0 讨论(0)
  • 2020-12-16 10:31

    Listen for the error event on the server isntance

    hons_server.on 'error', (err) ->
        console.log 'there was an error:', err.message
    
    0 讨论(0)
提交回复
热议问题