Skip to main content
D
Node.js
Node.js Error Handling

Node.js Error Handling

Node.js/Express error handling prevents server crashes.

Read Time
5 min read
Difficulty
Beginner
Last Updated
Jun 15, 2026
Version
v1.0.0

Introduction

Express provides a simple way to catch any errors that happen anywhere on the server. Developers write a special middleware function that catches bad requests, missing files, or broken code, and sends a polite error message to the user instead of crashing.

Example

Example
1/* this catches errors */
2app.use((err, req, res, next) => {
3  console.error(err.message);
4  res.status(500).send('Something broke on the server!');
5});

Key Points

  • Error handling middleware takes four arguments instead of three.
  • The arguments are `(err, req, res, next)`.
  • It belongs at the very bottom of the file, after all other routes.
  • Sending a 500 status code tells the browser the server failed.

Up Next

Continue your journey with the next topic.

Go to Deployment