📖 Scenario: You are building a simple Express web server. You want to handle requests to pages that do not exist by showing a friendly 404 Not Found message.
🎯 Goal: Create an Express app that responds with a 404 Not Found message for any request that does not match existing routes.
📋 What You'll Learn
Create an Express app instance
Add a basic route for the home page at '/'
Add a 404 handler middleware that catches all unmatched routes
Send a 404 status and a simple text message '404 Not Found' in the 404 handler
💡 Why This Matters
🌍 Real World
Web servers often need to handle requests to pages that do not exist. Showing a clear 404 Not Found message helps users understand the problem.
💼 Career
Knowing how to add middleware and handle 404 errors is a basic skill for backend developers working with Express or similar web frameworks.
Progress0 / 4 steps
1
Create Express app and home route
Write code to import Express, create an app instance called app, and add a GET route for '/' that sends the text 'Home Page'.
Express
Hint
Use express() to create the app. Use app.get for the home route.
2
Add 404 handler middleware
Add a middleware function using app.use that takes req, res, and next parameters. This middleware will handle all unmatched routes.
Express
Hint
Use app.use with three parameters to catch all unmatched routes.
3
Set 404 status and send message
Inside the 404 middleware, set the response status to 404 using res.status(404) and send the text '404 Not Found' using res.send.
Express
Hint
Chain res.status(404) and res.send('404 Not Found') to respond properly.
4
Start the server on port 3000
Add code to start the Express server by calling app.listen on port 3000.
Express
Hint
Use app.listen(3000) to start the server.
Practice
(1/5)
1. What is the main purpose of a 404 Not Found handler in an Express app?
easy
A. To catch requests for routes that do not exist and respond with a message
B. To handle database connection errors
C. To log all incoming requests
D. To serve static files like images and CSS
Solution
Step 1: Understand what a 404 error means
A 404 error means the requested page or route was not found on the server.
Step 2: Role of the 404 handler in Express
The 404 handler catches these unknown routes and sends a clear message to the user.
Final Answer:
To catch requests for routes that do not exist and respond with a message -> Option A
Quick Check:
404 handler = catch unknown routes [OK]
Hint: 404 handler catches unknown routes last in middleware [OK]
Common Mistakes:
Confusing 404 handler with error handler for server errors
Placing 404 handler before route definitions
Using 404 handler to serve static files
2. Which of the following is the correct syntax to add a 404 handler in Express?
easy
A. app.route('*').get((req, res) => { res.status(404).send('Not Found'); });
B. app.get('/404', (req, res) => { res.send('Not Found'); });
C. app.use((req, res) => { res.status(404).send('Not Found'); });
D. app.use((err, req, res, next) => { res.status(404).send('Not Found'); });
Solution
Step 1: Identify the middleware signature for 404 handler
A 404 handler uses app.use with (req, res) parameters, no error parameter.
Step 2: Check each option's correctness
app.use((req, res) => { res.status(404).send('Not Found'); }); correctly uses app.use with (req, res) and sets status 404. app.get('/404', (req, res) => { res.send('Not Found'); }); is a route, not a catch-all. app.use((err, req, res, next) => { res.status(404).send('Not Found'); }); is an error handler (has err param). app.route('*').get((req, res) => { res.status(404).send('Not Found'); }); uses app.route('*') which is not standard for 404.
Final Answer:
app.use((req, res) => { res.status(404).send('Not Found'); }); -> Option C
Quick Check:
404 handler = app.use with (req, res) [OK]
Hint: 404 handler uses app.use with (req, res), no error param [OK]
Common Mistakes:
Using error handler signature (err, req, res, next) for 404
Defining 404 as a specific route like '/404'
Using app.route('*') incorrectly for 404
3. Given this Express code snippet, what will the server respond with when a user visits /unknown?