How to handle errors with Express .listen() (in Typescript)?

后端 未结 2 957
南方客
南方客 2021-02-13 20:35

Currently converting my project to use Typescript. My previously working code to launch Express in Node looks like this:

server.listen(port, (error) => {
  if         


        
2条回答
  •  孤街浪徒
    2021-02-13 21:27

    I ran into this today, too. Here's the (breaking) change to the type definition:

    https://github.com/DefinitelyTyped/DefinitelyTyped/pull/47063

    That PR's author suggests that the proper way to handle listen errors is by registering a listener (as linked by Jonas above):

    https://nodejs.org/api/net.html#net_server_listen

    It sounds like the type definitions were a lie, and that parameter didn't really exist, so removing its use from your app should be a no-op. That's what I'm doing. (furthermore, I'm okay with errors like EADDRINUSE crashing my app, so I'm not adding any new error handling)

提交回复
热议问题