Add Basic Error Handling to an Express 5 App

Jamund Ferguson
InstructorJamund Ferguson
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

In this lesson you'll learn how to add a custom 404 handler to your express app. You'll also learn how to define a general error handler that is called when any of your routes or middleware have errors.

Express provides an excellent reference on error handling in express on the Express website.

404 Handler

A 404 error handler is basically a route handler defined after all of the others without a path associated with it. These handlers naturally become a catchall for any request that isn't handled by existing routes. You define it like this:

app.use(function(req, res) {
     res.status(404).send("Not Found");

Read more in the express API doc.

Error Handler

Express allows you to register a general error handler that catches any errors thrown in the course of running your application* including both routes and middlewares. It's common to think of this as your 500 error handler, which is the HTTP status code for Internal Server Error. Think of this as something going wrong that is not the users fault.

To register the error handler call app.use with a function taking these 4 arguments: err, req, res, next as in this short example:

app.use(function(err, req, res, next) {
    console.error(err.stack); // log the error to the STDERR
    res.send(err.message); // send back a friendly message to the client

Note: express error handlers can't usually catch errors thrown in an async callback function such as a setTimeout call or process.nextTick function. Errors thrown in these situations will typically crash your server.

Jamund Ferguson: [0:00] After all your other routes, type app.use, pass in a function with req and res arguments, and inside that function type res.status(404).send("Not found"). Because this is the final route we register with our application and because we didn't pass in any kind of path to limit where it should be called, this route will function as a catchall for any request made to our server.

[0:22] If a request is not handled by an existing route, such as "/" here, it will be handled by this one. Let's take a look at how that works.

[0:28] Start our server and hit our server at a normal route. It works. We hit a route that we know doesn't exist, like 404, and we'll see it gets a 404 status code and it returns Not found. Of course, this works for any nonexistent route. Idontexist, Not found, 404.

[0:46] Back in our index.js file, type app.get("/error") and inside of our route handler here type throw new Error("oops I made a mistake"). Save the file, start the server. Let's hit /error and see what happens by default when an error's thrown in one of our routes.

[1:04] We've got a bunch of HTML that gets returned and inside that it looks like our entire stack trace is showing up there for our users. It does look like we logged there, which is pretty nice.

[1:15] Let's add a custom error handler for the situation. At the end of our file type app.use(function). This function is going to take four arguments, err, req, res, and next. It has to have all four of those arguments for Express to recognize it as an error handler.

[1:31] The first thing that we want to do is log the stack trace, and instead of using const.log, type console.error(err.stack). Console.error will log it just as we might see it normally, but instead of sending it to standard out, it gets sent to standard error, which is helpful if you want to store all of your errors together in a single log file.

[1:50] Now that we've logged it, we still need to return something to the client. We don't want to turn a bunch of HTML, so let's call res.status(500).send(err.message). Let's save this. Start the server. Oops I made a mistake. We've logged the stack trace of the error, and you can see the log here shows that we have a 500 status code that was sent.

~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today