Bird
0
0

Consider this Node.js Express code snippet:

medium📝 Predict Output Q4 of 15
Rest API - HTTP Status Codes
Consider this Node.js Express code snippet:
app.get('/data', (req, res) => {
  try {
    const result = someUndefinedFunction();
    res.json(result);
  } catch (error) {
    res.status(500).send('Server error');
  }
});
What will the server respond with when a client requests /data?
AA JSON response with the result of someUndefinedFunction.
BA 500 status with message 'Server error'.
CA 404 Not Found error.
DA 200 OK with empty response.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the try block

    Calling someUndefinedFunction() causes a runtime error because it is not defined.
  2. Step 2: Check catch block behavior

    The error is caught, and the server responds with status 500 and message 'Server error'.
  3. Final Answer:

    A 500 status with message 'Server error'. -> Option B
  4. Quick Check:

    Undefined function error triggers 500 response [OK]
Quick Trick: Undefined functions cause 500 errors caught by try-catch [OK]
Common Mistakes:
  • Assuming undefined function returns empty or 200
  • Confusing 404 with 500 errors
  • Ignoring try-catch error handling

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes