-
-
Notifications
You must be signed in to change notification settings - Fork 16.8k
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
app.listen() error behavior is different in Express 4 vs. 5, if port is occupied #6191
Comments
Although I don't know for sure, the code indicates that this is on purpose. Comparing the implementation of the Lines 633 to 636 in 59fc270
... with Lines 609 to 617 in 344b022
... the code now explicitly invokes the user-provided callback (if it exists) in the event of an error. I'm guessing this is to provide the user with more control over their errors, instead of being subject to a crashing app. However, I agree with you that this transfer of error-handling responsibility to the user in the |
Adds a note to the `migrating-5` guide alerting users that error events received by a server will now cause the callback argument in `app.listen` to be invoked. In Express 4, this was not the case and errors would be thrown. Resolves expressjs/express#6191 Signed-off-by: Dustin Popp <dustinpopp@ibm.com>
Okay probably the Express user is now required to check the 1st argument of the callback. const server = app.listen(7072, '0.0.0.0', (error) => {
if (error) {
// This line is only reachable in Express 5, but not in Express 4.
console.error("Failed to start server (Express 5)");
throw error;
}
console.log('Listening on http://localhost:7072/ - e.g. http://localhost:7072/api/health');
}); Works fine:
|
Adds a note to the `migrating-5` guide alerting users that error events received by a server will now cause the callback argument in `app.listen` to be invoked. In Express 4, this was not the case and errors would be thrown. Resolves expressjs/express#6191 Signed-off-by: Dustin Popp <dustinpopp@ibm.com> Co-authored-by: M. Heide <66078329+heidemn-faro@users.noreply.github.com>
Adds a note to the `migrating-5` guide alerting users that error events received by a server will now cause the callback argument in `app.listen` to be invoked. In Express 4, this was not the case and errors would be thrown. Resolves expressjs/express#6191 Signed-off-by: Dustin Popp <dustinpopp@ibm.com> Co-authored-by: M. Heide <66078329+heidemn-faro@users.noreply.github.com>
Hi there,
Great to see that Express 5 with async request handlers is finally available :-)
I have a question about behavior that differs in Express 4 vs. 5, if the listen port is already in use.
I wanted to know if that's on purpose, or if it's a bug.
If it's on purpose, it should probably be mentioned here? https://expressjs.com/en/guide/migrating-5.html
Thanks for having a look!
Enviroment: WSL2, Ubuntu 22.04, Node 20 or 22
Results with Express 4, if port is already in use:
Results with Express 5, if port is already in use:
The text was updated successfully, but these errors were encountered: