Yang Chandler
Yang Chandler

Reputation: 49

How can I use errorhandler in nodejs connect

I`ve tried to use errorhandler, but there is no deference between using errorhandler or not. I wonder how to use errorhandler module in nodejs.

var app = connect();
if (process.env.NODE_ENV === 'development') {

  console.log('development on');
  app.use(errorhandler({log: errorNotification}));
}

function errorNotification(err, str, req) {
  var title = 'Error in ' + req.method + ' ' + req.url;
  console.log('title');
}

app.use( function (req,res) {
   res.writeHead(200,{'Content-Type': 'text/html'});
   aaaaa(); //to invoke Reference error
   res.end('Not Hello');
});

Upvotes: 0

Views: 228

Answers (1)

Leonid Beschastny
Leonid Beschastny

Reputation: 51490

The problem here is that connect applies middlewares in the order you registered them. So, your errorhandler can only handle errors from previously registered middlewares. In most cases error handling middleware should be the last one:

app.use( function (req,res) {
  // whatever
});

app.use(errorhandler({log: errorNotification}));

Upvotes: 1

Related Questions