Bird
Raised Fist0
Expressframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does req.body represent in an Express app?
easy
A. The data sent by the client in the request payload
B. The URL parameters of the request
C. The headers of the HTTP request
D. The response sent back to the client

Solution

  1. Step 1: Understand the role of req.body

    req.body holds the data sent by the client inside the request payload, such as form data or JSON.
  2. Step 2: Differentiate from other request parts

    URL parameters are in req.params, headers in req.headers, and response is separate from request.
  3. Final Answer:

    The data sent by the client in the request payload -> Option A
  4. Quick Check:

    req.body = client data [OK]
Hint: Remember: body means client data sent inside request [OK]
Common Mistakes:
  • Confusing req.body with URL parameters
  • Thinking req.body holds response data
  • Assuming headers are in req.body
2. Which middleware must you use to correctly parse JSON data into req.body?
easy
A. express.urlencoded()
B. express.static()
C. express.raw()
D. express.json()

Solution

  1. Step 1: Identify middleware for JSON parsing

    To parse JSON payloads, Express provides express.json() middleware.
  2. Step 2: Differentiate from other middleware

    express.urlencoded() parses URL-encoded data, express.static() serves files, and express.raw() parses raw buffer data.
  3. Final Answer:

    express.json() -> Option D
  4. Quick Check:

    JSON parsing middleware = express.json() [OK]
Hint: Use express.json() to read JSON in req.body [OK]
Common Mistakes:
  • Using express.urlencoded() for JSON data
  • Not adding any middleware before accessing req.body
  • Confusing express.static() with body parsing
3. Given this Express route handler, what will console.log(req.body) output if the client sends JSON {"name":"Alice","age":30}?
app.post('/user', express.json(), (req, res) => {
  console.log(req.body);
  res.send('Received');
});
medium
A. "{\"name\":\"Alice\",\"age\":30}"
B. undefined
C. {"name":"Alice","age":30}
D. An error is thrown

Solution

  1. Step 1: Identify the loop range

    The route uses express.json() middleware, so JSON payload is parsed into an object.
  2. Step 2: Understand req.body content

    Since the client sends JSON {"name":"Alice","age":30}, req.body will be the parsed object with keys and values.
  3. Final Answer:

    {"name":"Alice","age":30} -> Option C
  4. Quick Check:

    JSON parsed to object = logged req.body [OK]
Hint: If express.json() is used, req.body is parsed object [OK]
Common Mistakes:
  • Expecting req.body to be a string
  • Forgetting to use express.json() middleware
  • Thinking req.body is undefined without middleware
4. What is the main reason req.body is undefined in this Express app?
const express = require('express');
const app = express();

app.post('/data', (req, res) => {
  console.log(req.body);
  res.send('Done');
});
medium
A. Missing middleware to parse the request body
B. The route path is incorrect
C. The HTTP method should be GET instead of POST
D. The server is not listening on a port

Solution

  1. Step 1: Identify the loop range

    The code does not use any middleware like express.json() or express.urlencoded() to parse the request body.
  2. Step 2: Understand effect on req.body

    Without parsing middleware, Express does not fill req.body, so it remains undefined.
  3. Final Answer:

    Missing middleware to parse the request body -> Option A
  4. Quick Check:

    No body parser = req.body undefined [OK]
Hint: Always add body parsing middleware before using req.body [OK]
Common Mistakes:
  • Assuming route path or method causes undefined body
  • Not adding express.json() or express.urlencoded()
  • Thinking server listen affects req.body
5. You want to accept both JSON and URL-encoded form data in your Express app and access it via req.body. Which setup correctly handles both types of payloads?
hard
A. app.use(express.raw()); app.use(express.urlencoded({ extended: false }));
B. app.use(express.json()); app.use(express.urlencoded({ extended: true }));
C. app.use(express.static()); app.use(express.json());
D. app.use(express.urlencoded()); app.use(express.json({ extended: true }));

Solution

  1. Step 1: Identify middleware for JSON and URL-encoded data

    express.json() parses JSON payloads, and express.urlencoded({ extended: true }) parses URL-encoded form data.
  2. Step 2: Confirm correct order and options

    Using both middleware with correct options ensures req.body works for both data types.
  3. Final Answer:

    app.use(express.json()); app.use(express.urlencoded({ extended: true })); -> Option B
  4. Quick Check:

    Use both json() and urlencoded() middleware [OK]
Hint: Add both express.json() and express.urlencoded() middleware [OK]
Common Mistakes:
  • Using express.urlencoded() without options
  • Mixing middleware order incorrectly
  • Using express.static() instead of body parsers
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard