0
0
Expressframework~20 mins

Request body transformation in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Request Body Transformer
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Express middleware?
Consider this Express middleware that modifies the request body before passing control to the next handler. What will be the value of req.body in the next middleware?
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();
});
app.post('/test', (req, res) => {
  res.json({ transformedName: req.body.name });
});
AIf sent {"name": " alice "}, response is {"transformedName": "alice"}
BIf sent {"name": " alice "}, response is {"transformedName": null}
CIf sent {"name": " alice "}, response is {"transformedName": " alice "}
DIf sent {"name": " alice "}, response is {"transformedName": "ALICE"}
Attempts:
2 left
💡 Hint
Think about how the middleware modifies the req.body.name before the final handler.
📝 Syntax
intermediate
2:00remaining
Which option correctly parses JSON body and adds a new field?
You want to parse JSON request bodies and add a new field timestamp with the current time before handling the request. Which code snippet correctly does this in Express?
A
app.use((req, res, next) => {
  req.body = JSON.parse(req.body);
  req.body.timestamp = Date.now();
  next();
});
B
app.use(express.json());
app.use((req, res, next) => {
  req.body.timestamp = Date.now();
  next();
});
C
app.use(express.urlencoded());
app.use((req, res, next) => {
  req.body.timestamp = Date.now();
  next();
});
D
;)}
;)(txen  
;)(won.etaD = pmatsemit.ydob.qer  
{ >= )txen ,ser ,qer((esu.ppa
;))(nosj.sserpxe(esu.ppa
Attempts:
2 left
💡 Hint
Remember to parse JSON before accessing req.body as an object.
🔧 Debug
advanced
2:00remaining
Why does this middleware not modify the request body as expected?
This middleware tries to uppercase a field in the request body but the change is not reflected in the final handler. Why?
Express
app.use((req, res, next) => {
  req.body.name.toUpperCase();
  next();
});
app.post('/user', (req, res) => {
  res.json({ name: req.body.name });
});
ABecause <code>res.json()</code> is called before the middleware.
BBecause <code>toUpperCase()</code> returns a new string but does not change <code>req.body.name</code> in place.
CBecause <code>next()</code> is called before modifying <code>req.body.name</code>.
DBecause <code>req.body</code> is undefined without JSON parsing middleware.
Attempts:
2 left
💡 Hint
Remember how string methods like toUpperCase() work in JavaScript.
state_output
advanced
2:00remaining
What is the final value of req.body after this middleware chain?
Given these middlewares, what will req.body contain when the final handler runs?
Express
app.use(express.json());
app.use((req, res, next) => {
  req.body = { ...req.body, added: true };
  next();
});
app.use((req, res, next) => {
  delete req.body.removed;
  next();
});
app.post('/data', (req, res) => {
  res.json(req.body);
});
AIf sent {"name":"bob","removed":123}, response is {"added":true}
BIf sent {"name":"bob","removed":123}, response is {"name":"bob","removed":123,"added":true}
CIf sent {"name":"bob","removed":123}, response is {"name":"bob","added":true}
DIf sent {"name":"bob","removed":123}, response is {"name":"bob"}
Attempts:
2 left
💡 Hint
Look at how the object is copied and the removed property is deleted.
🧠 Conceptual
expert
3:00remaining
Which option best explains why modifying req.body directly in middleware can be risky?
In Express, why might directly changing req.body in middleware cause unexpected issues in later handlers or middleware?
ABecause <code>req.body</code> might be shared by other middleware expecting the original structure, so changes can break assumptions.
BBecause <code>req.body</code> is immutable and cannot be changed once parsed.
CBecause Express automatically resets <code>req.body</code> after each middleware.
DBecause modifying <code>req.body</code> requires asynchronous operations to be safe.
Attempts:
2 left
💡 Hint
Think about how middleware chains share the same request object.