Bird
0
0

What will be the status code returned by this Express handler?

medium📝 Predict Output Q4 of 15
Rest API - HTTP Status Codes
What will be the status code returned by this Express handler?
app.get('/item', (req, res) => {
if (!req.query.id) {
res.status(400).send('Missing id');
} else {
res.status(200).json({ id: req.query.id });
}
});
A200 if id is present, 400 if missing
BAlways 200
CAlways 400
D500 if id is missing
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the if condition

    If the query parameter 'id' is missing, the code sends status 400 with message.
  2. Step 2: Analyze the else branch

    If 'id' is present, it sends status 200 with JSON data.
  3. Final Answer:

    200 if id is present, 400 if missing -> Option A
  4. Quick Check:

    Conditional status codes = A [OK]
Quick Trick: Check conditions to know which status code is sent [OK]
Common Mistakes:
MISTAKES
  • Assuming always 200
  • Confusing 400 with 500
  • Ignoring else branch

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes