Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
JSON Request and Response Patterns with Express
📖 Scenario: You are building a simple Express server that handles JSON data from clients. This server will receive JSON requests and send JSON responses.
🎯 Goal: Create an Express server that accepts JSON data in a POST request and responds with a JSON message confirming the received data.
📋 What You'll Learn
Create an Express app instance
Use middleware to parse JSON request bodies
Create a POST route at /submit that reads JSON data from the request body
Send a JSON response confirming the received data
💡 Why This Matters
🌍 Real World
Many web applications use Express servers to handle JSON data sent from frontend clients or other services.
💼 Career
Understanding JSON request and response patterns is essential for backend developers working with APIs and web servers.
Progress0 / 4 steps
1
Set up Express app and import modules
Write code to import express and create an Express app instance called app.
Express
Hint
Use require('express') to import Express and then call express() to create the app.
2
Add JSON body parsing middleware
Add middleware to the app to parse incoming JSON request bodies using app.use(express.json()).
Express
Hint
Use app.use(express.json()) to enable JSON parsing for request bodies.
3
Create POST route to receive JSON data
Create a POST route at /submit using app.post. Inside the route handler, access the JSON data from req.body and store it in a variable called data.
Express
Hint
Use app.post('/submit', (req, res) => { ... }) and inside the function assign req.body to data.
4
Send JSON response confirming received data
Inside the POST route handler, send a JSON response using res.json with an object containing a message key that says "Data received" and a received key with the data variable.
Express
Hint
Use res.json({ message: "Data received", received: data }) to send the JSON response.
Practice
(1/5)
1. What does the express.json() middleware do in an Express app?
easy
A. It sends JSON responses to the client.
B. It parses incoming JSON request bodies automatically.
C. It logs JSON data to the console.
D. It validates JSON schema in requests.
Solution
Step 1: Understand middleware purpose
The express.json() middleware is designed to parse JSON data sent in the request body.
Step 2: Differentiate from response methods
Sending JSON responses is done by res.json(), not express.json().
Final Answer:
It parses incoming JSON request bodies automatically. -> Option B
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