O'Reilly logo

Mastering Web Application Development with Express by Alexandru Vlăduțu

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Handling errors with middleware

Each time we call the next function inside a middleware or a route handler with an error parameter, it delegates that to the error-handling middleware. Express allows us to plug in a custom error handler, but by default, it just displays the error stack while developing and shows an Internal Server Error message in production.

In the development mode, the error page isn't greatly formatted by default, but we can load the errorHandler() middleware that used to be bundled with Express (https://www.npmjs.org/package/errorhandler) to sweeten the deal. Let's create a sample application with that handler and include a middleware that calls next with an error argument:

var express = require('express'); var app = express(); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required