0
0
Expressframework~10 mins

Request body transformation in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Request body transformation
Client sends HTTP request with body
Express receives request
Middleware parses raw body
Middleware transforms body data
Route handler accesses transformed body
Response sent back to client
This flow shows how Express receives a request, parses and transforms the body, then the route handler uses the transformed data.
Execution Sample
Express
app.use(express.json());
app.use((req, res, next) => {
  if (req.body && req.body.name) {
    req.body.name = req.body.name.trim().toUpperCase();
  }
  next();
});
This code parses JSON request bodies and transforms the 'name' field by trimming spaces and converting to uppercase.
Execution Table
StepActionInput BodyTransformationOutput BodyNext
1Receive requestundefinedNone yetundefinedParse JSON body
2Parse JSON bodyundefinedParse raw JSON{"name": " alice "}Transform body
3Transform body{"name": " alice "}Trim and uppercase 'name'{"name": "ALICE"}Call next()
4Route handler access{"name": "ALICE"}Use transformed body{"name": "ALICE"}Send response
5Send responseN/AN/AN/AEnd
💡 Request processed fully; response sent back to client.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
req.bodyundefinedundefined{"name": " alice "}{"name": "ALICE"}{"name": "ALICE"}
Key Moments - 3 Insights
Why do we need to call next() after transforming the body?
Calling next() passes control to the next middleware or route handler, allowing the request to continue processing with the transformed body as shown in execution_table step 3.
What happens if express.json() middleware is missing?
Without express.json(), req.body remains undefined or raw, so the transformation middleware cannot access or modify the body, as seen between steps 1 and 2.
Why do we trim and uppercase the 'name' field?
Trimming removes extra spaces and uppercasing standardizes the format, making data consistent for later use, demonstrated in step 3 transformation.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is req.body after step 3?
Aundefined
B{"name": "ALICE"}
C{"name": " alice "}
D{"name": "alice"}
💡 Hint
Check the 'Output Body' column in row for step 3 in execution_table.
At which step does the JSON parsing happen?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in execution_table where 'Parse JSON body' is mentioned.
If we remove the transformation middleware, what would req.body be at step 4?
A{"name": " alice "}
B{"name": "ALICE"}
Cundefined
D{}
💡 Hint
Refer to variable_tracker and note that without transformation, req.body stays as parsed JSON.
Concept Snapshot
Express request body transformation:
- Use express.json() to parse JSON bodies.
- Middleware can modify req.body before route handlers.
- Always call next() to continue processing.
- Transformations can clean or standardize data.
- Route handlers access the transformed req.body.
Full Transcript
In Express, when a client sends a request with a body, the server first parses the raw body using middleware like express.json(). Then, custom middleware can transform the parsed body, for example trimming spaces and uppercasing a name field. After transformation, the route handler accesses the cleaned data to process the request. Calling next() in middleware is essential to pass control forward. This flow ensures the server works with consistent, clean data before responding.