Express catches errors in your routes and passes them to error-handling middleware: functions with four arguments, (err, req, res, next), added after your routes. Express has a default handler, but you'll want your own that logs the error and returns a clean response. The common problems are errors in async code that never reach the handler (so the request hangs or the process crashes), and handlers that respond without logging, hiding the cause.
What's the problem
Your Express app returns 500 errors, requests hang without a response, or the process crashes.
Why it happens
- Async errors not passed on, so they never reach the error handler.
- No error-handling middleware, or it's added before the routes.
- A response sent twice, causing "headers already sent" errors.
- An exception in a route, often a database or API call.
- The process crashing on an uncaught error. See Node crash.
How to fix it
- Add error-handling middleware with four arguments after all routes, and log
err.stackin it. - Make sure async errors reach it (pass them to
next(err)or use a version of Express that does this for promises). - Return after sending a response to avoid sending twice.
- Read the logs for the stack trace of the failing route.
- Return clear status codes: 400s for bad input, 500 only for real server failures.
What Preventionlabs checks
One click runs the full report on your domain. For this problem it shows whether your site loads, how fast, and where its redirects go; up to 50 pages from your sitemap, listing any that fail or are slow, plus broken links on them. With Cloudflare connected, it also shows which pages returned server errors (5xx) to visitors, and when; recent deploys and account changes, with their times. It doesn't read your server logs, so the exact error message is in your host's logs.
Run your free reportYour first report is free, no card needed. Then A$50 a month, up to 10 domains. Read-only: it never changes anything.
Sources
Preventionlabs is independent and not affiliated with Express. Product names are used only to describe the problem.
← All of Frameworks