📖 Scenario: You are building a simple Express server that needs to handle errors gracefully. To do this, you will create an error-handling middleware function with the correct signature so Express can recognize it and use it properly.
🎯 Goal: Create an error-handling middleware function in Express with the correct four-parameter signature: err, req, res, and next. This middleware should send a 500 status code and a JSON response with an error message.
📋 What You'll Learn
Create a basic Express app instance called app
Define an error-handling middleware function with parameters err, req, res, and next
Inside the middleware, send a 500 status code and a JSON response with { error: 'Internal Server Error' }
Use app.use to register the error-handling middleware
💡 Why This Matters
🌍 Real World
Error-handling middleware is essential in Express apps to catch and respond to errors gracefully, improving user experience and debugging.
💼 Career
Knowing how to write and register error-handling middleware is a key skill for backend developers working with Express.js.
Progress0 / 4 steps
1
Create Express app instance
Write a line to import Express and create an app instance called app using express().
Express
Hint
Use import express from 'express' and then const app = express().
2
Define error-handling middleware function
Write a function called errorHandler with parameters err, req, res, and next. Inside, send a 500 status code and a JSON response with { error: 'Internal Server Error' } using res.status(500).json().
Express
Hint
The error handler must have four parameters: err, req, res, and next. Use res.status(500).json({ error: 'Internal Server Error' }) inside.
3
Register error-handling middleware
Use app.use to register the errorHandler middleware function.
Express
Hint
Use app.use(errorHandler) to add the error handler to the app.
4
Add a test route that triggers an error
Add a GET route at /error that calls next(new Error('Test error')) to trigger the error handler.
Express
Hint
Use app.get('/error', (req, res, next) => { next(new Error('Test error')) }) to create the route.
Practice
(1/5)
1. Which of the following correctly describes the signature of an error-handling middleware function in Express?
easy
A. It has five parameters: err, req, res, next, done.
B. It has three parameters: req, res, next.
C. It has two parameters: req, res.
D. It has four parameters: err, req, res, next.
Solution
Step 1: Recall Express middleware function signatures
Normal middleware functions have three parameters: req, res, next. Error-handling middleware must have four parameters.
Step 2: Identify the error-handling middleware signature
Error-handling middleware functions always start with err as the first parameter, followed by req, res, next.
Final Answer:
It has four parameters: err, req, res, next. -> Option D
Quick Check:
Error middleware signature = err, req, res, next [OK]
Hint: Error middleware always has 4 parameters starting with err [OK]
Common Mistakes:
Using only three parameters for error middleware
Forgetting the err parameter at the start
Adding extra parameters beyond four
2. Which of the following is the correct syntax to define an error-handling middleware in Express?
easy
A. app.use(function(req, res, next, err) { /* code */ });
B. app.use(function(err, req, res, next) { /* code */ });
C. app.use(function(err, req, next, res) { /* code */ });
D. app.use(function(req, err, res, next) { /* code */ });
Solution
Step 1: Check parameter order in error middleware
The correct order is err, req, res, next. This order is required by Express to recognize error middleware.
Step 2: Match the correct syntax
app.use(function(err, req, res, next) { /* code */ }); matches the correct order and syntax for error-handling middleware.
C. The parameters are in the wrong order; err must be first.
D. res.status(500) is invalid inside error middleware.
Solution
Step 1: Check parameter order for error middleware
Error-handling middleware must have parameters in order: err, req, res, next. Here, err is last, which is incorrect.
Step 2: Understand impact of wrong parameter order
Express will not recognize this as error middleware, so errors won't be caught properly.
Final Answer:
The parameters are in the wrong order; err must be first. -> Option C
Quick Check:
err parameter must be first in error middleware [OK]
Hint: Error middleware must start with err parameter [OK]
Common Mistakes:
Placing err parameter anywhere but first
Assuming next() call is mandatory in all error middleware
Confusing error middleware with normal middleware signature
5. You want to create an error-handling middleware that logs the error and sends a JSON response with status 500. Which of the following is the best correct implementation?
Error middleware must have exactly four parameters in order: err, req, res, next. app.use((err, req, res, next) => { console.log(err); res.status(500).json({error: err.message}); }); matches this.
Step 2: Check response status and format
app.use((err, req, res, next) => { console.log(err); res.status(500).json({error: err.message}); }); sets status 500 and sends JSON with error message, which is correct and user-friendly.