Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Express app handle startup errors

I have app in Node.js and Express. I need to write tests for it. I have a problem with handling Express app errors. I found this How do I catch node.js/express server errors like EADDRINUSE?, but it doesn't work for me, I don't know why. I want to handle errors, which can occured while expressApp.listen() is executing (EADDRINUSE, EACCES etc.).

express = require('express') listener = express()  #doesn't work for me listener.on('uncaughtException', (err) ->   #do something )  #doesn't work too listener.on("error", (err) ->   #do something )  #this works, but it caughts all errors in process, I want only in listener process.on('uncaughtException', (err) ->   #do something )  listener.listen(80) #for example 80 to get error 

Any ideas?

like image 774
Piane_Ramso Avatar asked Nov 10 '12 14:11

Piane_Ramso


People also ask

How do you handle errors in express JS?

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.

Is ExpressJs deprecated?

It has been deprecated since v4. 11.0, and Express 5 no longer supports it at all.

What is next error in Express?

If you call next() with an error after you have started writing the response (for example, if you encounter an error while streaming the response to the client) the Express default error handler closes the connection and fails the request.


2 Answers

This should do the trick:

listener.listen(80).on('error', function(err) { }); 

What listener.listen actually does is create a HTTP server and call listen on it:

app.listen = function(){   var server = http.createServer(this);   return server.listen.apply(server, arguments); }; 
like image 55
Marius Tibeica Avatar answered Sep 22 '22 22:09

Marius Tibeica


First off, expressJS does not throw the uncaughtException event, process does, so it's no surprise your code doesn't work.

So use: process.on('uncaughtException',handler) instead.

Next, expressJS already provides a standard means of error handling which is to use the middleware function it provides for this purpose, as in:

app.configure(function(){     app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); }); 

This function returns an error message to the client, with optional stacktrace, and is documented at connectJS errorHandler.

(Note that errorHandler is actually part of connectJS and is only exposed by expressJS.)

If the behavior the existing errorHandler provides is not sufficient for your needs, its source is located at connectJS's errorHandler middleware and can be easily modified to suit your needs.

Of course, rather than modifying this function directly, the "correct" way to do this is to create your own errorHandler, using the connectJS version as a starting point, as in:

var myErrorHandler = function(err, req, res, next){     ...     // note, using the typical middleware pattern, we'd call next() here, but      // since this handler is a "provider", i.e. it terminates the request, we      // do not. }; 

And install it into expressJS as:

app.configure(function(){     app.use(myErrorHandler); }); 

See Just Connect it, Already for an explanation of connectJS's idea of filter and provider middleware and How To Write Middleware for Connect/Express for a well-written tutorial.

You might also find these useful:

  • How to handle code exceptions in node.js?

  • Recover from Uncaught Exception in Node.JS

Finally, an excellent source of information regarding testing expressJS can be found in its own tests.

like image 26
Rob Raisch Avatar answered Sep 21 '22 22:09

Rob Raisch