0
0
Expressframework~20 mins

req.body for request payload in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express req.body Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does req.body contain after a POST request?
Consider an Express server with the following middleware setup:
app.use(express.json());

If a client sends a POST request with JSON payload {"name":"Alice","age":30}, what will req.body contain inside the route handler?
Express
app.post('/user', (req, res) => {
  res.json(req.body);
});
AAn empty object {}
BA string containing '{"name":"Alice","age":30}'
CUndefined
D{'name': 'Alice', 'age': 30}
Attempts:
2 left
💡 Hint
Think about what express.json() middleware does to the incoming JSON payload.
📝 Syntax
intermediate
2:00remaining
Which code correctly accesses a nested property in req.body?
Given a POST request with JSON payload:
{"user": {"email": "test@example.com"}}

Which option correctly retrieves the email inside the route handler?
Express
app.post('/signup', (req, res) => {
  // Access email here
});
Aconst email = req.body.user.email;
Bconst email = req.body['user.email'];
Cconst email = req.body.user['email'];
Dconst email = req.body['user']['email'];
Attempts:
2 left
💡 Hint
Remember how to access nested object properties in JavaScript.
🔧 Debug
advanced
2:00remaining
Why is req.body undefined in this Express route?
Look at this Express server code:
const express = require('express');
const app = express();

app.post('/data', (req, res) => {
  res.send(req.body);
});

app.listen(3000);

When sending a JSON POST request, req.body is always undefined. Why?
AThe route handler is missing a return statement.
BThe POST request must use query parameters instead of JSON.
CThe server is missing middleware to parse JSON bodies like express.json().
DThe server must use app.use(express.urlencoded()) instead of express.json().
Attempts:
2 left
💡 Hint
Think about what Express needs to understand JSON payloads.
state_output
advanced
2:00remaining
What is the value of req.body after sending a form with application/x-www-form-urlencoded?
An Express app uses this middleware:
app.use(express.urlencoded({ extended: false }));

A client submits a form with fields username=joe and password=123. What will req.body contain inside the POST route?
A{'username': 'joe', 'password': '123'}
BA string 'username=joe&password=123'
CUndefined
DAn empty object {}
Attempts:
2 left
💡 Hint
What does express.urlencoded() middleware do?
🧠 Conceptual
expert
3:00remaining
Why should you never trust req.body data directly in Express?
When building an Express app, why is it important to validate or sanitize data from req.body before using it?
ABecause <code>req.body</code> only works with JSON, not other content types.
BBecause <code>req.body</code> can contain malicious data that may cause security issues like injection attacks.
CBecause <code>req.body</code> is always empty unless validated first.
DBecause Express automatically encrypts <code>req.body</code> data, so you must decrypt it.
Attempts:
2 left
💡 Hint
Think about what happens if users send unexpected or harmful data.