0
0
Expressframework~20 mins

POST route handling in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express POST 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 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?
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}!`);
});
A"Hello, Alice!"
B"Hello, undefined!"
CSyntaxError: Unexpected token in JSON
DError: Cannot read property 'name' of undefined
Attempts:
2 left
💡 Hint
Remember to use express.json() middleware to parse JSON bodies.
📝 Syntax
intermediate
2: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?
A
app.use(express.urlencoded());
app.post('/submit', (req, res) => {
  res.send(req.body.username);
});
B
app.use(express.json());
app.post('/submit', (req, res) => {
  res.send(req.body.username);
});
C
app.post('/submit', (req, res) => {
  res.send(req.body.username);
});
D
app.use(express.urlencoded({ extended: true }));
app.post('/submit', (req, res) => {
  res.send(req.body.username);
});
Attempts:
2 left
💡 Hint
URL-encoded form data needs a specific middleware with extended option.
🔧 Debug
advanced
2: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'?
Express
const express = require('express');
const app = express();

app.post('/data', (req, res) => {
  const id = req.body.id;
  res.send(`ID is ${id}`);
});
ABecause express.json() middleware is missing, so req.body is undefined.
BBecause req.body.id is not a valid property name.
CBecause the route path '/data' is incorrect for POST requests.
DBecause res.send() cannot send template literals.
Attempts:
2 left
💡 Hint
Check if the server can parse JSON bodies.
state_output
advanced
2: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'?
Express
const express = require('express');
const app = express();
let count = 0;

app.post('/click', (req, res) => {
  count += 1;
  res.send(`Count is ${count}`);
});
A0
B3
C1
Dundefined
Attempts:
2 left
💡 Hint
The variable 'count' is outside the route handler and increments on each call.
🧠 Conceptual
expert
2: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?
AMiddleware order does not matter; Express runs all middleware in parallel.
Bexpress.json() middleware must come before logging middleware to parse the body first.
CLogging middleware should be placed before express.json() middleware to log raw request data.
DLogging middleware should be placed after the route handler to log response data.
Attempts:
2 left
💡 Hint
Think about when you want to see the original request data in logs.