Express Error Handling

Registered members can download the FREE Get Started App. This is the project I used to compose articles about setting up VS Code and developing Node with Express and the Embedded JavaScript (EJS) view engine.

Proper error handling prevents the application from crashing. This article describes implementing not found and error handling middleware.

When I decided to learn Express, I found a lot of articles and AI suggestions which did not implement ES6 standards. ES6 or ECMAScript 2015 changed the landscape of JavaScript with the ability to create promises for asynchronous programming. I am familiar with ASP.NET Core and C# web applications and SQL Server databases. I developed this Express application with KenHaggerty.Com as a model. Registered members can download the FREE Get Started PostgreSQL app and Get Started MySQL app which implement this getting started with Express EJS tutorial.

Let's start by deliberately causing a not found error and an error from code. Add these button links to a public page like index.ejs.

index.ejs
<div class="row">
    <div class="col-12">
        <h4>Error Handling Testing:</h4>
        <a class="btn btn-primary mb-1" href="/no-route"><span>Not Found</span></a>
        <a class="btn btn-primary mb-1" href="/throw-error"><span>Throw Error</span></a>
    </div>
</div>

Then add a throw-error route to server.js where we throw a new Error from code.

server.js
// Throw error for testing
app.get('/throw-error', (req, res) => {
  throw new Error("Error handling test.");
});
Error Handling Buttons

The Not Found button calls a non-existent route. The result is a plain "Cannot GET /no-route".

Plain Cannot Get Route

The Throw Error button calls a route which throws a new Error. The result is stack trace with sensitive information.

Plain Stack Trace

You can provide better presentation if you add a simple error.ejs page.

error.ejs
<div class="text-center border-bottom mb-1">
    <h1 class="text-primary"><%= pageTitle %></h1>
    <h4><%= pageSubTitle %></h4>
</div>
<div class="row pt-4">
    <div class="col-12 text-center">
        <h4 class="text-danger"><%= errorMessage %></h4>
    </div>
</div>

And add middleware to handle route not found and error handling to server.js.

server.js
// Route not found middleware
app.use( async (req, res, next) => {    
  try {
    const templateData = { 
      pageTitle: 'Error Page', 
      pageSubTitle: 'This page provides information about an error.',
      errorMessage: 'Not found resource.'
    }
    res.status(404).render('pages/error', templateData);
  } catch (err) {
    next(err);
  }
});

// Custom error-handling middleware
app.use( async (err, req, res, next) => {
  try {    
    const templateData = { 
      pageTitle: 'Error Page', 
      pageSubTitle: 'This page provides information about an error.',
      errorMessage: err.message
    }
    res.status(500).render('pages/error', templateData);
  } catch(err) {
    console.error(err);
  }  
});

The Not Found error page.

Not Found Error Page

The Throw Error error page.

Throw Error Error Page
Created: 2/14/26