0
0
Expressframework~20 mins

Testing POST with request body in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express POST Body Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when sending a JSON body to this Express POST route?

Consider this Express route that handles POST requests with JSON body parsing enabled.

const express = require('express');
const app = express();
app.use(express.json());

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

If a client sends a POST request to /data with JSON body {"name": "Alice"}, what will the server respond with?

Express
const express = require('express');
const app = express();
app.use(express.json());

app.post('/data', (req, res) => {
  res.json({ received: req.body });
});
A{"received":{"name":"Alice"}}
B{"received":"{\"name\": \"Alice\"}"}
C{"received":null}
DSyntaxError
Attempts:
2 left
💡 Hint

Remember that express.json() middleware parses JSON body into an object.

📝 Syntax
intermediate
2:00remaining
Which option correctly parses URL-encoded POST body in Express?

You want to parse URL-encoded form data sent in a POST request body in Express. Which middleware setup is correct?

Express
const express = require('express');
const app = express();

// Which middleware to use here?

app.post('/submit', (req, res) => {
  res.send(req.body);
});
Aapp.use(express.json());
Bapp.use(express.urlencoded({ extended: true }));
Capp.use(bodyParser.json());
Dapp.use(express.text());
Attempts:
2 left
💡 Hint

URL-encoded form data is not JSON. Use the middleware that parses URL-encoded bodies.

🔧 Debug
advanced
2:00remaining
Why does this Express POST route not receive the JSON body?

Look at this Express app code:

const express = require('express');
const app = express();

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

app.use(express.json());

When sending a JSON POST request to /api, req.body is undefined. Why?

Express
const express = require('express');
const app = express();

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

app.use(express.json());
Ares.json() cannot send objects
Bexpress.json() middleware is missing
Cexpress.json() middleware is used after the route, so body is not parsed yet
Dreq.body is always undefined in POST routes
Attempts:
2 left
💡 Hint

Middleware order matters in Express.

state_output
advanced
2:00remaining
What is the value of req.body after sending a plain text POST request?

Given this Express setup:

const express = require('express');
const app = express();
app.use(express.text());

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

If a client sends a POST request to /text with body Hello World (plain text), what will req.body contain?

Express
const express = require('express');
const app = express();
app.use(express.text());

app.post('/text', (req, res) => {
  res.send(req.body);
});
A"Hello World"
B{"Hello World"}
Cundefined
DSyntaxError
Attempts:
2 left
💡 Hint

express.text() parses plain text bodies as strings.

🧠 Conceptual
expert
3:00remaining
Which middleware combination correctly handles JSON and URL-encoded POST bodies simultaneously?

You want your Express app to accept POST requests with either JSON or URL-encoded form data bodies. Which middleware setup correctly supports both?

Express
const express = require('express');
const app = express();

// Middleware setup here

app.post('/submit', (req, res) => {
  res.json(req.body);
});
Aapp.use(express.raw()); app.use(express.urlencoded({ extended: true }));
Bapp.use(express.urlencoded({ extended: false })); app.use(express.json());
Capp.use(express.text()); app.use(express.json());
Dapp.use(express.json()); app.use(express.urlencoded({ extended: false }));
Attempts:
2 left
💡 Hint

Order of middleware matters, but both JSON and URL-encoded parsers must be included.