0
0
Expressframework~10 mins

Request body transformation in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to parse JSON request bodies in Express.

Express
const express = require('express');
const app = express();

app.use(express.[1]());

app.post('/data', (req, res) => {
  res.send(req.body);
});
Drag options to blanks, or click blank then click option'
Aurlencoded
Bjson
Ctext
Draw
Attempts:
3 left
💡 Hint
Common Mistakes
Using express.urlencoded() instead of express.json() for JSON data.
Forgetting to add the middleware before route handlers.
2fill in blank
medium

Complete the code to transform the request body by adding a new property before sending the response.

Express
app.post('/transform', (req, res) => {
  const data = req.body;
  data.[1] = 'processed';
  res.json(data);
});
Drag options to blanks, or click blank then click option'
Aresult
Btype
Cprocessed
Dstatus
Attempts:
3 left
💡 Hint
Common Mistakes
Using a value as a property name instead of a key.
Trying to modify req.body directly without assigning to a variable.
3fill in blank
hard

Fix the error in the code to correctly parse URL-encoded form data.

Express
app.use(express.[1]({ extended: true }));
Drag options to blanks, or click blank then click option'
Ajson
Btext
Curlencoded
Draw
Attempts:
3 left
💡 Hint
Common Mistakes
Using express.json() for form data.
Omitting the middleware and causing req.body to be undefined.
4fill in blank
hard

Fill both blanks to create a middleware that transforms the request body by uppercasing a 'name' property.

Express
app.use((req, res, next) => {
  if (req.body && req.body.[1]) {
    req.body.[2] = req.body.name.toUpperCase();
  }
  next();
});
Drag options to blanks, or click blank then click option'
Aname
Busername
Cuser
DName
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong property name in the condition.
Not calling next() to continue the middleware chain.
5fill in blank
hard

Fill all three blanks to create a route that transforms the request body by adding a timestamp and sending the modified object.

Express
app.post('/submit', (req, res) => {
  const data = req.body;
  data.[1] = new Date().[2]();
  res.[3](data);
});
Drag options to blanks, or click blank then click option'
Atimestamp
BtoISOString
Cjson
Dnow
Attempts:
3 left
💡 Hint
Common Mistakes
Using Date.now() which returns a number instead of a string.
Forgetting to send the response with res.json().