Bird
Raised Fist0
Node.jsframework~20 mins

Parsing request body (JSON and form data) in Node.js - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Body Parsing Master
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.js JSON body parser example?
Consider this Express.js route that uses the built-in JSON parser middleware. What will be the value of req.body after a POST request with JSON {"name":"Alice","age":30}?
Node.js
import express from 'express';
const app = express();
app.use(express.json());
app.post('/user', (req, res) => {
  res.json(req.body);
});
A{"name":"Alice","age":30}
B"{\"name\":\"Alice\",\"age\":30}"
C{}
Dundefined
Attempts:
2 left
💡 Hint
Think about what express.json() middleware does to the incoming request body.
📝 Syntax
intermediate
2:00remaining
Which option correctly parses URL-encoded form data in Express.js?
You want to parse form data sent with content type application/x-www-form-urlencoded. Which middleware usage is correct?
Node.js
import express from 'express';
const app = express();
// Middleware to parse form data here
app.post('/submit', (req, res) => {
  res.json(req.body);
});
Aapp.use(express.form());
Bapp.use(express.json({ extended: true }));
Capp.use(express.urlencoded());
Dapp.use(express.urlencoded({ extended: true }));
Attempts:
2 left
💡 Hint
Check the correct middleware function name and options for parsing URL-encoded data.
🔧 Debug
advanced
2:00remaining
Why does req.body remain empty when sending JSON data?
Given this Express.js setup, why is req.body empty after sending JSON data in a POST request?
Node.js
import express from 'express';
const app = express();
app.post('/data', (req, res) => {
  res.json(req.body);
});
app.use(express.json());
Aexpress.json() does not parse JSON data by default; you must enable it explicitly.
BThe request must have content type text/plain for express.json() to work.
CThe middleware express.json() is used after the route, so it doesn't parse the body before the route handler.
Dreq.body is always empty unless you use express.urlencoded() middleware.
Attempts:
2 left
💡 Hint
Middleware order matters in Express.js.
state_output
advanced
2:00remaining
What is the value of req.body after sending multipart/form-data without middleware?
You send a POST request with Content-Type: multipart/form-data but do not use any middleware to parse it. What will req.body contain?
Node.js
import express from 'express';
const app = express();
app.post('/upload', (req, res) => {
  res.json(req.body);
});
AParsed form fields as an object
Bundefined
CRaw multipart data as a string
D{} (empty object)
Attempts:
2 left
💡 Hint
Express does not parse multipart/form-data by default.
🧠 Conceptual
expert
3:00remaining
Which middleware setup correctly handles JSON and URL-encoded form data in Express.js?
You want your Express.js app to accept both JSON and URL-encoded form data in POST requests. Which setup correctly parses both types before route handlers?
Aapp.use(express.json()); app.use(express.urlencoded({ extended: false }));
Bapp.use(express.urlencoded({ extended: true })); app.use(express.json());
Capp.use(express.json({ extended: true })); app.use(express.urlencoded());
Dapp.use(express.urlencoded()); app.use(express.json({ limit: '1mb' }));
Attempts:
2 left
💡 Hint
Check the correct order and options for both middlewares.

Practice

(1/5)
1. What does express.json() middleware do in a Node.js Express app?
easy
A. It serves static JSON files from the server.
B. It parses URL query parameters and stores them in req.query.
C. It encrypts JSON data before sending it to the client.
D. It parses incoming JSON request bodies and makes the data available in req.body.

Solution

  1. Step 1: Understand express.json() purpose

    This middleware parses JSON data sent in the request body, converting it into a JavaScript object.
  2. Step 2: Know where parsed data is stored

    The parsed object is assigned to req.body so route handlers can access it easily.
  3. Final Answer:

    It parses incoming JSON request bodies and makes the data available in req.body. -> Option D
  4. Quick Check:

    express.json() parses JSON body = C [OK]
Hint: express.json() parses JSON body into req.body [OK]
Common Mistakes:
  • Confusing express.json() with parsing URL query parameters
  • Thinking it serves static files
  • Assuming it encrypts data
2. Which middleware correctly parses URL-encoded form data in Express?
easy
A. app.use(express.urlencoded({ extended: true }))
B. app.use(express.json())
C. app.use(express.static('public'))
D. app.use(bodyParser.raw())

Solution

  1. Step 1: Identify middleware for form data

    express.urlencoded() parses URL-encoded form data sent by HTML forms.
  2. Step 2: Check correct usage

    Using { extended: true } allows rich objects and arrays to be encoded.
  3. Final Answer:

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

    express.urlencoded() parses form data = A [OK]
Hint: Use express.urlencoded() for form data parsing [OK]
Common Mistakes:
  • Using express.json() for form data
  • Confusing static file serving with parsing
  • Using bodyParser.raw() instead of urlencoded
3. Given this Express route, what will console.log(req.body) output if the client sends JSON {"name":"Alice"}?
app.use(express.json());
app.post('/user', (req, res) => {
  console.log(req.body);
  res.send('Received');
});
medium
A. { name: 'Alice' }
B. undefined
C. An empty object {}
D. A string '{"name":"Alice"}'

Solution

  1. Step 1: Middleware parses JSON body

    express.json() converts JSON string into a JavaScript object and assigns it to req.body.
  2. Step 2: Logging req.body shows parsed object

    console.log prints the object { name: 'Alice' }.
  3. Final Answer:

    { name: 'Alice' } -> Option A
  4. Quick Check:

    express.json() parses JSON to object = B [OK]
Hint: express.json() parses JSON string to object in req.body [OK]
Common Mistakes:
  • Expecting req.body to be a string
  • Not using express.json() middleware
  • Assuming req.body is undefined
4. Why does req.body remain undefined in this Express app?
const express = require('express');
const app = express();

app.post('/submit', (req, res) => {
  console.log(req.body);
  res.send('Done');
});

app.listen(3000);
medium
A. Because the server is not listening on port 3000.
B. Because the route path is incorrect.
C. Because the app is missing middleware to parse the request body.
D. Because console.log cannot print req.body.

Solution

  1. Step 1: Check middleware usage

    The app does not use express.json() or express.urlencoded(), so request bodies are not parsed.
  2. Step 2: Understand req.body behavior

    Without parsing middleware, req.body is undefined because Express does not parse request bodies by default.
  3. Final Answer:

    Because the app is missing middleware to parse the request body. -> Option C
  4. Quick Check:

    Missing body parser middleware = D [OK]
Hint: Always add body parser middleware to access req.body [OK]
Common Mistakes:
  • Assuming req.body is always available
  • Blaming route path or console.log
  • Ignoring middleware setup
5. You want to accept both JSON and URL-encoded form data in your Express app. Which setup correctly parses both types so req.body works for all POST requests?
hard
A. app.use(express.urlencoded({ extended: false })); app.use(express.json());
B. app.use(express.json()); app.use(express.urlencoded({ extended: true }));
C. app.use(express.json()); app.use(express.urlencoded({ extended: false }));
D. app.use(bodyParser.json()); app.use(bodyParser.urlencoded());

Solution

  1. Step 1: Identify middleware for both data types

    express.json() parses JSON bodies; express.urlencoded({ extended: true }) parses form data with rich objects.
  2. Step 2: Confirm correct order and options

    Both middlewares should be used; extended: true allows nested objects in form data.
  3. Final Answer:

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

    Use both express.json() and express.urlencoded({ extended: true }) = A [OK]
Hint: Use both express.json() and express.urlencoded({ extended: true }) [OK]
Common Mistakes:
  • Using extended: false limits form data parsing
  • Using deprecated bodyParser package
  • Not including both middlewares