Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include listen errors in app.listen() callback #2623

Closed
wants to merge 1 commit into from

Conversation

wesleytodd
Copy link
Member

I know that you can listen on the server instance, but it seems like this might be nicer to just proxy up the error event. Thoughts?

@dougwilson dougwilson added the pr label Apr 19, 2015
@dougwilson
Copy link
Contributor

You can call app.listen multiple times, for example listening on multiple ports. How will a user know which server caused the error?

This is also pretty breaking, because no one has been adding error event listeners on app, and now, even if they added the appropriate listening to the returned server, their app will crash.

@dougwilson dougwilson self-assigned this Apr 19, 2015
@dougwilson
Copy link
Contributor

Also, if you were just adding this for the listening error, the .listen method accepts a callback so you can just handle the error there:

app.listen(3000, function (err) {
  // ... do stuff
})

@wesleytodd
Copy link
Member Author

@dougwilson Ok, thats cool, it was just a one line change so I figured I would do it and see your thoughts. Your points make perfect sense, I guess I have just never used multiple listens (I use nginx for ssl term).

For the callback scenario, maybe I am doing something wrong, but the EADDRINUSE error causes an uncaught exception and crashes the server. Here is my code:

process.on('uncaughtException', function(err) {
    console.error('Uncaught: ', err);
});
app.listen(app.get('port'), app.get('hostname'), function(err) {
  if (err) {
    return console.error('Error starting server', err);
  }
  console.log('Listeneing');
});

And the output is:

$ node index.js
Uncaught:  { [Error: listen EADDRINUSE 127.0.0.1:3001]
  code: 'EADDRINUSE',
  errno: 'EADDRINUSE',
  syscall: 'listen',
  address: '127.0.0.1',
  port: 3001 }

This situation is the only reason I thought this might be a nicer api, since the best one is apparently not working correctly now.

@dougwilson
Copy link
Contributor

For the callback scenario, maybe I am doing something wrong, but the EADDRINUSE error causes an uncaught exception and crashes the server.

Oops, I was not remembering correctly. The callback function is actually passed as the listening event listener within Node.js. Perhaps a patch that would make the callback given to Express's listen method callback on the first listening or error event may be desirable? The module ee-first should make this trivial :)

@wesleytodd
Copy link
Member Author

Ignoring the fact that i didn't change the tests, what do you think of this: wesleytodd@a8bc849

Also ignore the tabs, I would fix those up before submitting. :)

@dougwilson
Copy link
Contributor

In general, looks fine. I would skip manipulating the arguments object directly and just copy to an intermediate array. Otherwise, I'd accept this PR with this new style :)

@wesleytodd
Copy link
Member Author

Ok, hows that?

@dougwilson dougwilson changed the title Proxy server errors to express app Include listen errors in app.listen() callback Apr 19, 2015
if (typeof args[args.length - 1] === 'function'){
var done = Array.prototype.pop.call(args);
eeFirst([[server, 'error', 'listening']], function(err, ee, event, eventArgs){
done.apply(this, eventArgs);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably use the same context as the listening event does: against ee instead of app.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call

@wesleytodd
Copy link
Member Author

Let me know if you want me to change anything else!!

@dougwilson
Copy link
Contributor

Thanks, @wesleytodd , looks fine and targeted for 4.13 :)

@dougwilson dougwilson added this to the 4.13 milestone Jun 19, 2015
@dougwilson dougwilson mentioned this pull request Jun 19, 2015
6 tasks
var app = express();
var app2 = express();

var server = app.listen(9999, function(err){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this port number be made dynamic? Getting some race conditions where the Express suite is running multiples times on the same machine and they all want to use port 9999. Just have app listen randomly and have app2 listen to whatever port app is listening on should work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I can make this change for sure. Let me know if you also want me to move this PR to the 5.0 branch and I will do it all at once.

@dougwilson dougwilson modified the milestones: 5.0, 4.13 Jun 19, 2015
@dougwilson dougwilson added 5.x and removed 4.x labels Jun 19, 2015
@dougwilson
Copy link
Contributor

Ok, cool, I got your changes all fixed up :) I found while testing that there are some issues with adding this in 4.x, namely that people are unconditionally assuming the callback means the server is up. I definitely love this convenience and will have it land in 5.0 instead \m/

@wesleytodd
Copy link
Member Author

Sounds good!! let me know if there is anything else you need from me, but it looks like it is all good!

@dougwilson dougwilson force-pushed the master branch 2 times, most recently from 5f268a4 to 9848645 Compare July 31, 2015 20:58
@wesleytodd
Copy link
Member Author

If this is slated for 5, should I re-make this PR against the 5.0 branch? Or are you pulling in the change manually? If so I can close this out.

@wesleytodd
Copy link
Member Author

Closed in favor of #3216

@wesleytodd wesleytodd closed this Feb 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants