What if your website could gently catch every wrong turn visitors take?
Why 404 Not Found handler in Express? - Purpose & Use Cases
Imagine you build a website with many pages, but when someone types a wrong address, nothing happens or the server crashes.
Without a proper 404 handler, users get confused or see ugly errors. Manually checking every URL is slow and messy.
A 404 Not Found handler in Express catches all unknown requests and shows a friendly message automatically.
app.get('/home', (req, res) => { res.send('Home'); }); // no catch for wrong URLs
app.use((req, res) => { res.status(404).send('Page not found'); });This lets your app gracefully handle missing pages and guide users back on track.
When you mistype a URL on a shopping site, a 404 page helps you find your way instead of showing a blank screen.
Manual URL checks are hard and error-prone.
Express 404 handler catches all unknown routes automatically.
Users get clear feedback and better experience.
Practice
404 Not Found handler in an Express app?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 AQuick Check:
404 handler = catch unknown routes [OK]
- Confusing 404 handler with error handler for server errors
- Placing 404 handler before route definitions
- Using 404 handler to serve static files
Solution
Step 1: Identify the middleware signature for 404 handler
A 404 handler usesapp.usewith (req, res) parameters, no error parameter.Step 2: Check each option's correctness
app.use((req, res) => { res.status(404).send('Not Found'); }); correctly usesapp.usewith (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'); }); usesapp.route('*')which is not standard for 404.Final Answer:
app.use((req, res) => { res.status(404).send('Not Found'); }); -> Option CQuick Check:
404 handler = app.use with (req, res) [OK]
- Using error handler signature (err, req, res, next) for 404
- Defining 404 as a specific route like '/404'
- Using app.route('*') incorrectly for 404
/unknown?
const express = require('express');
const app = express();
app.get('/home', (req, res) => {
res.send('Welcome Home');
});
app.use((req, res) => {
res.status(404).send('Page Not Found');
});
app.listen(3000);Solution
Step 1: Check defined routes
The only defined route is '/home'. The request is for '/unknown', which is not defined.Step 2: Check 404 handler behavior
Theapp.usemiddleware catches all unmatched routes and sends 'Page Not Found' with status 404.Final Answer:
'Page Not Found' with status 404 -> Option DQuick Check:
Unknown route triggers 404 handler [OK]
- Assuming default 200 status for unknown routes
- Expecting server error instead of 404
- Thinking the request will hang without response
app.use((req, res, next) => {
res.send('404 Not Found');
res.status(404);
});Solution
Step 1: Check order of response methods
Callingres.sendsends the response immediately. Setting status after sending has no effect.Step 2: Correct order for setting status and sending
Status must be set beforeres.sendto apply correctly.Final Answer:
The status should be set before sending the response -> Option AQuick Check:
Set status before send [OK]
- Setting status after res.send (ignored)
- Using error handler signature for 404
- Using app.get instead of app.use for 404
Solution
Step 1: Choose correct middleware signature for 404 handler
404 handler usesapp.usewith (req, res), no error parameter.Step 2: Ensure status is set before sending JSON and logging happens before response
app.use((req, res) => { console.log('Missing:', req.url); res.status(404).json({ error: 'Not Found' }); }); logs the URL, sets status 404, and sends JSON correctly. app.use((err, req, res, next) => { console.log('Missing:', req.url); res.status(404).json({ error: 'Not Found' }); }); uses error handler signature incorrectly. app.get('*', (req, res) => { console.log('Missing:', req.url); res.status(404).json({ error: 'Not Found' }); }); uses app.get('*') which is less common and may miss some requests. app.use((req, res, next) => { res.json({ error: 'Not Found' }); console.log('Missing:', req.url); res.status(404); }); sets status after sending response and logs after response, which is wrong.Final Answer:
app.use((req, res) => { console.log('Missing:', req.url); res.status(404).json({ error: 'Not Found' }); }); -> Option BQuick Check:
404 handler logs then sends JSON with status 404 [OK]
- Using error handler signature for 404
- Setting status after sending response
- Logging after sending response
- Using app.get('*') instead of app.use
Ready to prove it?
Pass the test and this pattern moves unknown
