Challenge - 5 Problems
Express POST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when sending JSON data to this POST route?
Consider this Express POST route that receives JSON data and responds with a message including the user's name.
What will the server respond with if the client sends {"name": "Alice"} in the request body?
What will the server respond with if the client sends {"name": "Alice"} in the request body?
Express
const express = require('express'); const app = express(); app.use(express.json()); app.post('/greet', (req, res) => { const userName = req.body.name; res.send(`Hello, ${userName}!`); });
Attempts:
2 left
💡 Hint
Remember to use express.json() middleware to parse JSON bodies.
✗ Incorrect
The express.json() middleware parses the JSON body and makes it available as req.body. Since the client sends {"name": "Alice"}, req.body.name is 'Alice', so the response is 'Hello, Alice!'.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a POST route that reads form data?
You want to create a POST route in Express that reads URL-encoded form data sent by a client.
Which code snippet correctly sets up the middleware and route handler?
Which code snippet correctly sets up the middleware and route handler?
Attempts:
2 left
💡 Hint
URL-encoded form data needs a specific middleware with extended option.
✗ Incorrect
To parse URL-encoded form data, express.urlencoded middleware with { extended: true } is required. Option D correctly uses this middleware and accesses req.body.username.
🔧 Debug
advanced2:00remaining
Why does this POST route handler cause an error when receiving JSON?
Examine this Express POST route code:
Why does sending JSON data to '/data' cause the server to crash with 'TypeError: Cannot read property 'id' of undefined'?
Why does sending JSON data to '/data' cause the server to crash with 'TypeError: Cannot read property 'id' of undefined'?
Express
const express = require('express'); const app = express(); app.post('/data', (req, res) => { const id = req.body.id; res.send(`ID is ${id}`); });
Attempts:
2 left
💡 Hint
Check if the server can parse JSON bodies.
✗ Incorrect
Without express.json() middleware, Express does not parse JSON request bodies, so req.body is undefined. Accessing req.body.id causes a TypeError.
❓ state_output
advanced2:00remaining
What is the final value of 'count' after these POST requests?
This Express app counts how many times the '/click' POST route is called.
After three POST requests to '/click', what is the value of 'count'?
After three POST requests to '/click', what is the value of 'count'?
Express
const express = require('express'); const app = express(); let count = 0; app.post('/click', (req, res) => { count += 1; res.send(`Count is ${count}`); });
Attempts:
2 left
💡 Hint
The variable 'count' is outside the route handler and increments on each call.
✗ Incorrect
The variable 'count' starts at 0 and increments by 1 on each POST request to '/click'. After 3 requests, count is 3.
🧠 Conceptual
expert2:00remaining
Which statement about Express POST route middleware order is true?
In Express, you want to handle POST requests with JSON bodies and also log each request.
Which statement correctly describes the middleware order to ensure both logging and JSON parsing work properly?
Which statement correctly describes the middleware order to ensure both logging and JSON parsing work properly?
Attempts:
2 left
💡 Hint
Think about when you want to see the original request data in logs.
✗ Incorrect
Middleware runs in the order defined. To log raw request data, logging middleware should come before express.json() which modifies req.body. This way logs show original data.