Bird
0
0

Why does req.body remain undefined in a Node.js Express app even after sending JSON data in a POST request?

hard📝 Conceptual Q10 of 15
Node.js - HTTP Module
Why does req.body remain undefined in a Node.js Express app even after sending JSON data in a POST request?
ABecause body parsing middleware like express.json() is not used
BBecause req.body only works with form data, not JSON
CBecause POST requests do not have bodies by default
DBecause req.body is a read-only property
Step-by-Step Solution
Solution:
  1. Step 1: Understand body parsing in Express

    Express does not parse request bodies automatically; middleware like express.json() is required.
  2. Step 2: Explain why req.body is undefined

    Without middleware, req.body remains undefined even if JSON data is sent.
  3. Final Answer:

    Because body parsing middleware like express.json() is not used -> Option A
  4. Quick Check:

    Use express.json() to parse JSON body [OK]
Quick Trick: Add express.json() middleware to parse JSON body [OK]
Common Mistakes:
  • Thinking req.body works automatically
  • Believing req.body only works for form data
  • Assuming POST requests lack bodies

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes