0
0
Expressframework~30 mins

Request body transformation in Express - Mini Project: Build & Apply

Choose your learning style9 modes available
Request Body Transformation in Express
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use res.json() to send a JSON response.