0
0
Expressframework~10 mins

404 Not Found handler in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add a 404 handler middleware in Express.

Express
app.use(function(req, res, [1]) {
  res.status(404).send('Page not found');
});
Drag options to blanks, or click blank then click option'
Anext
Berror
Crequest
Dresponse
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'error' instead of 'next' as the third parameter.
Using 'request' or 'response' instead of the next function.
2fill in blank
medium

Complete the code to set the 404 status code before sending the response.

Express
app.use(function(req, res, next) {
  res.[1](404).send('Not Found');
});
Drag options to blanks, or click blank then click option'
AsetStatus
Bstatus
Ccode
DsendStatus
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'code' or 'setStatus' which are not Express methods.
Using 'sendStatus' which sends the status and ends the response immediately.
3fill in blank
hard

Fix the error in the 404 handler to correctly handle unmatched routes.

Express
app.use(function(req, res, next) {
  res.status(404).[1]('Sorry, page not found');
});
Drag options to blanks, or click blank then click option'
Asend
Bwrite
Cjson
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'write' without ending the response causes hanging requests.
Using 'end' without sending content results in empty response.
Using 'json' sends JSON, not plain text.
4fill in blank
hard

Fill both blanks to create a 404 handler that renders a 'notfound' view with status 404.

Express
app.use(function(req, res, next) {
  res.[1](404);
  res.[2]('notfound');
});
Drag options to blanks, or click blank then click option'
Astatus
Bsend
Crender
Djson
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' instead of 'render' to display a view.
Using 'json' which sends JSON instead of rendering a view.
5fill in blank
hard

Fill all three blanks to create a 404 handler that logs the URL, sets status 404, and sends a message.

Express
app.use(function(req, res, next) {
  console.log('404 at: ' + [1]);
  res.[2](404);
  res.[3]('Page not found');
});
Drag options to blanks, or click blank then click option'
Areq.url
Bstatus
Csend
Dreq.path
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'req.path' instead of 'req.url' logs only the path, not query string.
Using 'sendStatus' instead of separate 'status' and 'send' calls.
Forgetting to set status before sending response.