0
0
Expressframework~20 mins

404 Not Found handler in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express 404 Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when a request hits this 404 handler?
Consider this Express 404 handler middleware. What will the client receive if they request a non-existent route?
Express
app.use((req, res) => {
  res.status(404).send('Page not found');
});
AThe client receives a 500 status with an error message.
BThe client receives a 200 status with the text 'Page not found'.
CThe client receives a 404 status with an empty response body.
DThe client receives a 404 status with the text 'Page not found'.
Attempts:
2 left
💡 Hint
Look at the status code set and the response sent.
📝 Syntax
intermediate
2:00remaining
Which 404 handler code snippet is syntactically correct?
Identify the valid Express 404 handler middleware from the options below.
A
app.use((req, res, next) => {
  res.status(404).send('Not found');
});
B
app.use((req, res) => {
  res.status(404).send('Not found');
});
C
app.use((req, res, next) => {
  res.status(404).send('Not found')
);
D
app.use((req, res, next) => {
  res.status(404).send('Not found');
);
Attempts:
2 left
💡 Hint
Check for balanced parentheses and braces.
🔧 Debug
advanced
2:00remaining
Why doesn't this 404 handler run for requests to '/hello'?
Given this Express app code, why does the 404 handler not get called when requesting '/hello'?
Express
app.get('/hello', (req, res) => {
  res.send('Hello world');
});

app.use((req, res) => {
  res.status(404).send('Not found');
});

app.use((err, req, res, next) => {
  res.status(500).send('Server error');
});
ABecause the 404 handler is placed before the route handlers.
BBecause the route '/hello' matches the request, so 404 handler doesn't run for that path.
CBecause the 404 handler is missing the error parameter, so it is treated as normal middleware.
DBecause the 404 handler does not call next(), so it blocks other middleware.
Attempts:
2 left
💡 Hint
Think about which routes match requests and middleware order.
state_output
advanced
2:00remaining
What is the response status and body for this 404 handler with next() call?
Analyze this Express 404 handler. What will the client receive?
Express
app.use((req, res, next) => {
  res.status(404);
  next();
});

app.use((req, res) => {
  res.send('Page missing');
});
AStatus 404 with empty body.
BStatus 200 with body 'Page missing'.
CStatus 404 with body 'Page missing'.
DStatus 200 with empty body.
Attempts:
2 left
💡 Hint
Check how status and next() affect response.
🧠 Conceptual
expert
3:00remaining
Which statement about Express 404 handlers is true?
Select the correct statement about how Express handles 404 Not Found errors.
AA 404 handler should be placed after all route handlers and middleware to catch unmatched requests.
BExpress automatically sends a 404 response if no middleware handles a request, so no 404 handler is needed.
CA 404 handler must have four parameters (err, req, res, next) to catch unmatched routes.
DA 404 handler can only send JSON responses, not plain text or HTML.
Attempts:
2 left
💡 Hint
Think about middleware order and parameters.