Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

server.listen(port, (error) => {
  if (error) throw error;
  console.info(`Ready on port ${port}`);
});

With this I now get a Typescript error:

Argument of type '(error: any) => void' is not assignable to parameter of type '() => void'.

I've tried assigning a type to the argument such as error: Error or error: any but this doesn't solve the problem. What am I doing wrong here?

Also, since I'm new to Typescript, whilst I've found plenty of resources for learning Typescript generally, is there any where I should be looking to know how to deal with Typescript in scenarios more specific to npm packages?

like image 784
CaribouCode Avatar asked May 24 '19 11:05

CaribouCode


People also ask

How do you handle errors in TypeScript?

Typescript and Javascript provide an error handling strategy based on the try/catch syntax which allows the programmer to escape the normal flow of the program in the presence of errors.

How do you handle errors in Express?

The simplest way of handling errors in Express applications is by putting the error handling logic in the individual route handler functions. We can either check for specific error conditions or use a try-catch block for intercepting the error condition before invoking the logic for handling the error.

What is listen method in Express?

listen() function is used to bind and listen the connections on the specified host and port. This method is identical to Node's http. Server. listen() method. If the port number is omitted or is 0, the operating system will assign an arbitrary unused port, which is useful for cases like automated tasks (tests, etc.).

How do I use Express router in TypeScript?

ts to use these routes : import express from 'express'; import dotenv from 'dotenv'; import { routes } from './routes'; const app = express(); dotenv. config(); // routes app. use('/', routes); // start the server app.


2 Answers

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.

like image 63
Jonas Wilms Avatar answered Sep 28 '22 07:09

Jonas Wilms


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)

like image 21
jrr Avatar answered Sep 28 '22 08:09

jrr