Back to case studies
Node.jsFundamental

Parsing incoming JSON request bodies in Express

Scenario

Context An Express API endpoint receives POST requests with a JSON body but the request body is always undefined. The Problem The express.json() middleware is not configured, so incoming request bodies are never parsed. Current Code app.post("/users", (req, res) => { console.log(req.body); // undefined res.sendStatus(200); }); Constraints Add the express.json() middleware globally before route definitions. Validate that required fields (name, email) are present in the body. Return a 400 error with a message if validation fails. Expected Deliverable An Express endpoint correctly parsing JSON request bodies, with basic field validation and appropriate error responses.

Targeted skills

nodejsexpressmiddlewarejsonvalidation
    Parsing incoming JSON request bodies in Express — Node.js hands-on scenario | CodeDuel