4. What is wrong with this Express code snippet for handling JSON requests?
app.post('/data', (req, res) => {
const data = req.body;
res.json({ received: data });
});
medium
A. Missing express.json() middleware to parse JSON body.
B. Using res.json() instead of res.send().
C. Incorrect route method; should be app.get.
D. No error; code works fine.
Solution
Step 1: Check JSON parsing middleware
The code accesses req.body but does not show express.json() middleware usage.
Step 2: Understand middleware necessity
Without express.json(), req.body will be undefined for JSON requests.
Final Answer:
Missing express.json() middleware to parse JSON body. -> Option A
Quick Check:
express.json() needed to parse JSON requests [OK]
Hint: Always add express.json() to parse JSON bodies [OK]
Common Mistakes:
Assuming req.body is parsed automatically
Confusing res.json() with res.send()
Using wrong HTTP method for JSON POST
5. You want to create an Express route that accepts a JSON array of numbers in the request body and responds with a JSON object containing the sum of those numbers. Which code snippet correctly implements this?
hard
A. app.post('/sum', express.json(), (req, res) => {
const numbers = req.body;
const sum = numbers.reduce((a, b) => a + b, 0);
res.json({ sum });
});
B. app.post('/sum', (req, res) => {
const numbers = JSON.parse(req.body);
const sum = numbers.reduce((a, b) => a + b, 0);
res.json({ sum });
});
C. app.post('/sum', express.urlencoded(), (req, res) => {
const numbers = req.body;
const sum = numbers.reduce((a, b) => a + b, 0);
res.json({ sum });
});
D. app.post('/sum', (req, res) => {
const numbers = req.body.numbers;
const sum = numbers.reduce((a, b) => a + b, 0);
res.send({ sum });
});
Solution
Step 1: Use express.json() middleware to parse JSON array
app.post('/sum', express.json(), (req, res) => {
const numbers = req.body;
const sum = numbers.reduce((a, b) => a + b, 0);
res.json({ sum });
}); correctly uses express.json() middleware inline to parse the JSON array in the request body.
Step 2: Sum array and send JSON response
It sums the numbers with reduce and sends the result with res.json(), which sets correct headers.
Step 3: Identify errors in other options
app.post('/sum', (req, res) => {
const numbers = JSON.parse(req.body);
const sum = numbers.reduce((a, b) => a + b, 0);
res.json({ sum });
}); tries to parse req.body manually, which is already parsed by middleware. app.post('/sum', express.urlencoded(), (req, res) => {
const numbers = req.body;
const sum = numbers.reduce((a, b) => a + b, 0);
res.json({ sum });
}); uses express.urlencoded() which is for form data, not JSON. app.post('/sum', (req, res) => {
const numbers = req.body.numbers;
const sum = numbers.reduce((a, b) => a + b, 0);
res.send({ sum });
}); accesses req.body.numbers but expects array directly in body; also uses res.send() which may not set JSON headers properly.
Final Answer:
app.post('/sum', express.json(), (req, res) => {
const numbers = req.body;
const sum = numbers.reduce((a, b) => a + b, 0);
res.json({ sum });
}); -> Option A
Quick Check:
express.json() + res.json() for JSON array sum [OK]
Hint: Use express.json() middleware and res.json() for JSON data [OK]
Common Mistakes:
Not using express.json() to parse JSON body
Using express.urlencoded() for JSON data
Manually parsing req.body with JSON.parse
Using res.send() instead of res.json() for JSON response