Complete the code to parse JSON request bodies in Express.
const express = require('express'); const app = express(); app.use(express.[1]()); app.post('/data', (req, res) => { res.send(req.body); });
Express uses express.json() middleware to parse JSON request bodies.
Complete the code to transform the request body by adding a new property before sending the response.
app.post('/transform', (req, res) => { const data = req.body; data.[1] = 'processed'; res.json(data); });
We add a new property named status to the request body object.
Fix the error in the code to correctly parse URL-encoded form data.
app.use(express.[1]({ extended: true }));To parse URL-encoded form data, use express.urlencoded() middleware.
Fill both blanks to create a middleware that transforms the request body by uppercasing a 'name' property.
app.use((req, res, next) => {
if (req.body && req.body.[1]) {
req.body.[2] = req.body.name.toUpperCase();
}
next();
});The middleware checks if req.body.name exists, then creates a new property username with the uppercase value.
Fill all three blanks to create a route that transforms the request body by adding a timestamp and sending the modified object.
app.post('/submit', (req, res) => { const data = req.body; data.[1] = new Date().[2](); res.[3](data); });
The code adds a timestamp property with the current date in ISO string format, then sends the data as JSON.