Currently converting my project to use Typescript. My previously working code to launch Express in Node looks like this:
server.listen(port, (error) => {
if
There is no error
¹.
server.listen(port, () => {
console.info(`Ready on port ${port}`);
});
To listen for errors, use server.listen(port).on("error", /*...*/)
².
¹: The docs are quite nested:
The Express docs say, that
This method is identical to Node’s http.Server.listen().
Now these docs say, that htt.Server.listen
equals Net.server.listen
.
And that then says:
This function is asynchronous. When the server starts listening, the 'listening' event will be emitted. The last parameter callback will be added as a listener for the 'listening' event.
Now the "listening" event does not seem to raise any error.
²: Thats the recommended way I found in the Express issuetracker.
Note that in most cases you don't want to handle the error, if the server crashes, it is very likely that the best option is to just restart the whole process.