📖 Scenario: You are building a simple Express server that receives user data in JSON format. You want to transform the incoming request body to add a new field before processing it further.
🎯 Goal: Build an Express server that accepts JSON data in a POST request, transforms the request body by adding a new field receivedAt with the current timestamp, and then sends back the transformed data as JSON.
📋 What You'll Learn
Create an Express app with JSON body parsing middleware
Add a POST route at /submit to receive JSON data
Add a receivedAt field with the current ISO timestamp to the request body
Send the transformed request body back as JSON in the response
💡 Why This Matters
🌍 Real World
Transforming request bodies is common in APIs to add metadata or normalize data before processing.
💼 Career
Backend developers often need to manipulate incoming data in Express servers to prepare it for storage or further logic.
Progress0 / 4 steps
1
Setup Express app and JSON middleware
Create an Express app by requiring express and calling express(). Use app.use(express.json()) to enable JSON body parsing.
Express
Hint
Use express.json() middleware to parse JSON bodies automatically.
2
Add POST route at /submit
Add a POST route handler on app for the path '/submit' using app.post('/submit', (req, res) => { ... }).
Express
Hint
Use app.post to create a POST route at /submit.
3
Transform request body by adding receivedAt
Inside the POST route handler, add a new field receivedAt to req.body with the current ISO timestamp using new Date().toISOString().
Express
Hint
Use new Date().toISOString() to get the current time in ISO format.
4
Send transformed body as JSON response
Send the transformed req.body back as JSON in the response using res.json(req.body).
Express
Hint
Use res.json() to send a JSON response.
Practice
(1/5)
1. What is the main purpose of request body transformation in Express?
easy
A. To send the response back to the client
B. To handle errors in the server
C. To change incoming data into a more useful format before using it
D. To set HTTP headers for the response
Solution
Step 1: Understand request body transformation
Request body transformation modifies the data received from the client to a better format for the app.
Step 2: Identify the main purpose
This process happens after parsing and helps clean or convert data before use.
Final Answer:
To change incoming data into a more useful format before using it -> Option C
Quick Check:
Request body transformation = change data format [OK]
Hint: Think: transform means change data format before use [OK]
Common Mistakes:
Confusing transformation with sending response
Mixing transformation with error handling
Thinking it sets HTTP headers
2. Which Express middleware is commonly used to parse JSON request bodies before transformation?
easy
A. express.urlencoded()
B. express.static()
C. express.raw()
D. express.json()
Solution
Step 1: Identify JSON parsing middleware
express.json() parses incoming JSON request bodies into JavaScript objects.
B. The check '!req.body.count' fails when count is 0
C. express.json() middleware is missing
D. res.send() should be called before res.status()
Solution
Step 1: Understand parseInt and check
parseInt converts strings to numbers; 0 is falsy in JavaScript.
Step 2: Analyze the if condition
The condition '!req.body.count' treats 0 as false, wrongly triggering the error for count=0.
Final Answer:
The check '!req.body.count' fails when count is 0 -> Option B
Quick Check:
Falsy check fails for 0 count [OK]
Hint: Check for 0 explicitly, not with '!value' [OK]
Common Mistakes:
Assuming parseInt is wrong here
Missing express.json() middleware
Calling res.send before res.status
5. You want to transform an incoming request body to remove any properties with empty strings and convert all numeric strings to numbers. Which code snippet correctly performs this transformation in Express?
hard
A. app.use(express.json());
app.post('/transform', (req, res) => {
for (const key in req.body) {
if (req.body[key] === '') delete req.body[key];
else if (!isNaN(req.body[key])) req.body[key] = Number(req.body[key]);
}
res.send(req.body);
});
B. app.use(express.json());
app.post('/transform', (req, res) => {
Object.keys(req.body).forEach(key => {
if (req.body[key] === '') req.body[key] = null;
else if (typeof req.body[key] === 'string') req.body[key] = parseInt(req.body[key]);
});
res.send(req.body);
});
C. app.use(express.urlencoded());
app.post('/transform', (req, res) => {
for (const key in req.body) {
if (req.body[key] === '') delete req.body[key];
else if (!isNaN(req.body[key])) req.body[key] = Number(req.body[key]);
}
res.send(req.body);
});
D. app.use(express.json());
app.post('/transform', (req, res) => {
for (const key in req.body) {
if (req.body[key] === '') req.body[key] = undefined;
else if (isNaN(req.body[key])) req.body[key] = Number(req.body[key]);
}
res.send(req.body);
});
Solution
Step 1: Check middleware and iteration
express.json() is needed to parse JSON bodies; for..in loops over keys correctly.
Step 2: Validate transformation logic
app.use(express.json());
app.post('/transform', (req, res) => {
for (const key in req.body) {
if (req.body[key] === '') delete req.body[key];
else if (!isNaN(req.body[key])) req.body[key] = Number(req.body[key]);
}
res.send(req.body);
}); deletes keys with empty strings and converts numeric strings to numbers using !isNaN check.
Step 3: Compare other options
app.use(express.json());
app.post('/transform', (req, res) => {
Object.keys(req.body).forEach(key => {
if (req.body[key] === '') req.body[key] = null;
else if (typeof req.body[key] === 'string') req.body[key] = parseInt(req.body[key]);
});
res.send(req.body);
}); sets empty strings to null and always parses strings with parseInt (may convert non-numeric wrongly). app.use(express.urlencoded());
app.post('/transform', (req, res) => {
for (const key in req.body) {
if (req.body[key] === '') delete req.body[key];
else if (!isNaN(req.body[key])) req.body[key] = Number(req.body[key]);
}
res.send(req.body);
}); uses express.urlencoded() which is wrong for JSON. app.use(express.json());
app.post('/transform', (req, res) => {
for (const key in req.body) {
if (req.body[key] === '') req.body[key] = undefined;
else if (isNaN(req.body[key])) req.body[key] = Number(req.body[key]);
}
res.send(req.body);
}); wrongly converts non-numeric strings to numbers.
Final Answer:
app.use(express.json());
app.post('/transform', (req, res) => {
for (const key in req.body) {
if (req.body[key] === '') delete req.body[key];
else if (!isNaN(req.body[key])) req.body[key] = Number(req.body[key]);
}
res.send(req.body);
}); -> Option A
Quick Check:
Delete empty and convert numeric strings = app.use(express.json());
app.post('/transform', (req, res) => {
for (const key in req.body) {
if (req.body[key] === '') delete req.body[key];
else if (!isNaN(req.body[key])) req.body[key] = Number(req.body[key]);
}
res.send(req.body);
}); [OK]