Skip to content

Commit

Permalink
docs: add v5 migration note for app.listen
Browse files Browse the repository at this point in the history
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 <[email protected]>
Co-authored-by: M. Heide <[email protected]>
  • Loading branch information
dpopp07 and heidemn-faro committed Dec 4, 2024
1 parent 9092d18 commit a042fc7
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions en/guide/migrating-5.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ You can then run your automated tests to see what fails, and fix problems accord
<li><a href="#path-syntax">Path route matching syntax</a></li>
<li><a href="#rejected-promises">Rejected promises handled from middleware and handlers</a></li>
<li><a href="#express.urlencoded">express.urlencoded</a></li>
<li><a href="#app.listen">app.listen</a></li>
<li><a href="#app.router">app.router</a></li>
<li><a href="#req.body">req.body</a></li>
<li><a href="#req.host">req.host</a></li>
Expand Down Expand Up @@ -159,6 +160,20 @@ Details of how Express handles errors is covered in the [error handling document

The `express.urlencoded` method makes the `extended` option `false` by default.

<h4 id="app.listen">app.listen</h4>

In Express 5, the `app.listen` method will invoke the user-provided callback function (if provided) when the server receives an error event. In Express 4, such errors would be thrown. This change shifts error-handling responsibility to the callback function in Express 5. If there is an error, it will be passed to the callback as an argument.
For example:

```js
const server = app.listen(8080, '0.0.0.0', (error) => {
if (error) {
throw error // e.g. EADDRINUSE
}
console.log(`Listening on ${JSON.stringify(server.address())}`)
})
```

<h4 id="app.router">app.router</h4>

The `app.router` object, which was removed in Express 4, has made a comeback in Express 5. In the new version, this object is a just a reference to the base Express router, unlike in Express 3, where an app had to explicitly load it.
Expand Down

0 comments on commit a042fc7

Please sign in to comment.