It is a middleware function that catches requests to routes that do not exist and sends a 404 status with a message to the client.
Click to reveal answer
beginner
Where should you place the 404 Not Found handler in your Express app?
It should be placed after all other route handlers so it only runs if no route matches the request.
Click to reveal answer
beginner
How do you send a 404 status code in Express?
Use res.status(404).send('Not Found') or res.status(404).json({ message: 'Not Found' }) inside the handler.
Click to reveal answer
beginner
Example code snippet for a simple 404 Not Found handler in Express?
app.use((req, res) => { res.status(404).send('404 Not Found'); });
Click to reveal answer
beginner
Why is it important to have a 404 Not Found handler in your Express app?
It improves user experience by clearly telling users when a page or resource does not exist, instead of leaving them with a blank or confusing error.
Click to reveal answer
Where should the 404 Not Found handler be placed in an Express app?
AIt does not matter
BBefore all route handlers
CIn the middle of route handlers
DAfter all other route handlers
✗ Incorrect
The 404 handler runs only if no other route matches, so it must be last.
What status code does a 404 Not Found handler send?
A404
B200
C500
D302
✗ Incorrect
404 means the requested resource was not found.
Which Express method sets the HTTP status code in the response?
Ares.status()
Bres.setStatus()
Cres.sendStatus()
Dres.code()
✗ Incorrect
res.status() sets the status code before sending the response.
What happens if you do not add a 404 handler in Express?
AExpress automatically sends a 404 response
BThe server crashes
CThe request hangs or returns a default response
DThe request is redirected
✗ Incorrect
Without a 404 handler, requests to unknown routes may hang or get a default empty response.
How do you define a 404 handler middleware in Express?
Aapp.get('/404', ...)
Bapp.use((req, res) => { ... })
Capp.post('/404', ...)
Dapp.route('/404')
✗ Incorrect
A 404 handler uses app.use with a function that handles unmatched requests.
Explain how to create and use a 404 Not Found handler in an Express app.
Think about middleware order and response status
You got /4 concepts.
Why is a 404 Not Found handler important for user experience in web apps?
Consider what happens if users get no feedback
You got /4 concepts.
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?