0
0
Node.jsframework~20 mins

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

Choose your learning style9 modes available
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.