📖 Scenario: You are building a simple Node.js server using Express. You want to handle errors gracefully so users get friendly messages instead of server crashes.
🎯 Goal: Create an Express app with a route that triggers an error, then add error-handling middleware to catch and respond with a JSON error message.
📋 What You'll Learn
Create an Express app instance called app
Add a route /cause-error that throws an error with message 'Something went wrong!'
Create error-handling middleware with four parameters: err, req, res, next
In the middleware, respond with status code 500 and JSON { error: err.message }
💡 Why This Matters
🌍 Real World
Error-handling middleware is essential in real web servers to prevent crashes and provide users with clear error messages.
💼 Career
Understanding error middleware is a key skill for backend developers working with Node.js and Express.
Progress0 / 4 steps
1
Set up Express app and error route
Create an Express app instance called app by requiring express and calling it. Then add a GET route /cause-error that throws a new Error with the message 'Something went wrong!'.
Node.js
Hint
Use const app = express() to create the app. Use app.get('/cause-error', (req, res) => { throw new Error('Something went wrong!'); }) to add the route.
2
Add a helper variable for the error status code
Create a constant called errorStatus and set it to 500. This will be used as the HTTP status code for errors.
Node.js
Hint
Write const errorStatus = 500; below the route.
3
Create error-handling middleware
Add error-handling middleware to the app using app.use. The middleware function must have four parameters: err, req, res, next. Inside, respond with status code errorStatus and send JSON with the key error and value err.message.
Node.js
Hint
Use app.use with a function that has err, req, res, next parameters. Inside, call res.status(errorStatus).json({ error: err.message }).
4
Start the server to listen on port 3000
Add code to make the app listen on port 3000 using app.listen. Provide a callback function that does nothing (empty arrow function).
Node.js
Hint
Use app.listen(3000, () => {}) to start the server.
Practice
(1/5)
1. What is the correct number of parameters for an error-handling middleware function in Node.js Express?
easy
A. One parameter: err
B. Three parameters: req, res, next
C. Two parameters: req, res
D. Four parameters: err, req, res, next
Solution
Step 1: Recall the error middleware signature
Error-handling middleware in Express always has four parameters: err, req, res, and next.
Step 2: Understand the role of each parameter
The first parameter is the error object, followed by the request, response, and next function to pass control.
Final Answer:
Four parameters: err, req, res, next -> Option D
Quick Check:
Error middleware = 4 parameters [OK]
Hint: Error middleware always has 4 parameters: err, req, res, next [OK]
Common Mistakes:
Using 3 parameters like normal middleware
Forgetting the err parameter
Placing error middleware before routes
2. Which of the following is the correct syntax to define an error-handling middleware in Express?