Bird
0
0

Identify the error in this Express.js route that should return 404 for missing products:

medium📝 Debug Q6 of 15
Rest API - HTTP Status Codes
Identify the error in this Express.js route that should return 404 for missing products:
app.get('/products/:id', (req, res) => {
  const products = { '10': 'Table', '20': 'Chair' };
  const product = products[req.params.id];
  if (product === undefined) {
    res.send(404, 'Product not found');
  } else {
    res.send(product);
  }
});
Ares.send(404, 'Product not found') is deprecated; use res.status(404).send()
BThe products object keys should be numbers, not strings
Creq.params.id should be converted to number before lookup
Dres.send() cannot send strings
Step-by-Step Solution
Solution:
  1. Step 1: Check usage of res.send with status code

    res.send(status, body) is deprecated in Express; status should be set with res.status()
  2. Step 2: Correct way to send 404

    Use res.status(404).send('Product not found') to set status and send message.
  3. Final Answer:

    res.send(404, 'Product not found') is deprecated; use res.status(404).send() -> Option A
  4. Quick Check:

    Use res.status(404).send() for 404 responses [OK]
Quick Trick: Use res.status(404).send() not res.send(404, ...) [OK]
Common Mistakes:
MISTAKES
  • Using deprecated res.send with status code
  • Not setting status code properly
  • Assuming string keys cause 404

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes