I've tried everything i found online,
How do I catch the address in use error?? (express 4.x)
Just wondering, why do you want to catch it? Isn't better if you "let it go"
I want to bunyan-log the error and then throw it
What version of Node.js are you using? With Express 4.13.3 on Node.js 0.12.1, I found your second option worked for me.
var express = require('express');
var http = require('http');
var app = express();
http.createServer(function(req, res) {}).on('error', console.log).listen(3000);
// These should all print the error object to console
http.createServer(function(req, res) {}).on('error', console.log).listen(3000);
http.createServer(function(req, res) {}).listen(3000).on('error', console.log);
app.listen(3000).on('error', console.log);
node test.js
{ [Error: listen EADDRINUSE] code: 'EADDRINUSE', errno: 'EADDRINUSE', syscall: 'listen' }
{ [Error: listen EADDRINUSE] code: 'EADDRINUSE', errno: 'EADDRINUSE', syscall: 'listen' }
{ [Error: listen EADDRINUSE] code: 'EADDRINUSE', errno: 'EADDRINUSE', syscall: 'listen' }
The Server object is a Node.js EventEmitter. As with all EventEmitter's, _most_ errors are passed to the 'error' event. If no handler is registered for the 'error' event, those bubble up to be thrown. try/catch does not work because when listen is attempted, any errors that occur are caught and emitted on the 'error' event using process.nextTick() -- that is, by the time the error is actually reported, the try/catch block has already exited. As a fallback, you can register an 'uncaughtException' handler on process as a catch all for any unhandled errors that occur on any EventEmitter object, but it's best to simply set the 'error' callback on the server object. @tunniclm's example ought to work for you.
The "proper" way to do this is:
var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app);
server.on('error', function (e) {
// do your thing
});
server.listen(3000);
Make the server yourself and bind the event before you start listening.
Hey guys,
it seems that running my server with "nodemon" causes different outcome than with "node".
nodeamon index.js I get the exception thrown to the console, and my error handler is not callednode index.js I get my handler calledanyone have any idea why?
I'm not that familiar with nodeamon but I would guess that it's likely hooking in it's own event listeners and taking actions before yours get a chance to fire.
I suspect that nodemon adds some error handler and handle the error for you (restart the process or wait for changes etc..). I took a quick look at the source code of nodemon, and found several places
For example https://github.com/remy/nodemon/blob/2f19ffb5a7162d08abbabea45ed995ac2cd9511a/lib/monitor/run.js#L101 and this https://github.com/remy/nodemon/blob/f5d1a04cc80f1bc963c20627177e545be42aeee7/lib/monitor/watch.js#L78
If you want to further investigate this issue, those two might contain some clues
Guys thanks for the help!
Most helpful comment
The "proper" way to do this is:
Make the server yourself and bind the event before you start listening.