Bird
0
0

Identify the error in this Express.js code that attempts to send a 301 redirect:

medium📝 Debug Q6 of 15
Rest API - HTTP Status Codes
Identify the error in this Express.js code that attempts to send a 301 redirect:
app.get('/old', (req, res) => {
  res.status(301);
  res.send('Redirecting');
  res.set('Location', '/new');
});
Ares.send() automatically sets Location header
BStatus code 301 is invalid for redirects
CLocation header should be set before sending response
Dres.status() must be called after res.send()
Step-by-Step Solution
Solution:
  1. Step 1: Check order of response methods

    Headers like Location must be set before sending the response body.
  2. Step 2: Identify the mistake in code order

    Here, res.send() is called before res.set('Location'), so Location header is not sent.
  3. Final Answer:

    Location header should be set before sending response -> Option C
  4. Quick Check:

    Set headers before sending response body [OK]
Quick Trick: Always set headers before sending response body [OK]
Common Mistakes:
MISTAKES
  • Setting headers after res.send()
  • Using wrong status code for redirect
  • Assuming res.send sets Location automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes