Bird
Raised Fist0
Expressframework~20 mins

POST route handling in Express - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does the app.post method in Express do?
easy
A. It defines a route that handles HTTP GET requests.
B. It defines a route that handles HTTP POST requests.
C. It starts the Express server.
D. It sends a response to the client automatically.

Solution

  1. Step 1: Understand HTTP methods in Express

    Express uses different methods like app.get and app.post to handle different HTTP request types.
  2. Step 2: Identify the purpose of app.post

    app.post specifically handles POST requests, which are used to send data to the server.
  3. Final Answer:

    It defines a route that handles HTTP POST requests. -> Option B
  4. Quick Check:

    POST route = app.post [OK]
Hint: POST routes handle data sent by clients via app.post [OK]
Common Mistakes:
  • Confusing POST with GET routes
  • Thinking app.post starts the server
  • Assuming app.post sends responses automatically
2. Which of the following is the correct syntax to create a POST route in Express for path /submit?
easy
A. app.post('/submit', (req, res) => res.send('Received'));
B. app.get('/submit', (req, res) => { res.send('Received'); });
C. app.post('/submit', (res, req) => { res.send('Received'); });
D. app.post('/submit', (req, res) => { res.send('Received'); });

Solution

  1. Step 1: Check method and path

    The method should be app.post and path '/submit'.
  2. Step 2: Verify callback parameters and syntax

    The callback function must have parameters (req, res) in that order, and the response should be sent inside the function body.
  3. Final Answer:

    app.post('/submit', (req, res) => { res.send('Received'); }); -> Option D
  4. Quick Check:

    Correct syntax = app.post('/submit', (req, res) => { res.send('Received'); }); [OK]
Hint: Use app.post(path, (req, res) => { }) with req first [OK]
Common Mistakes:
  • Swapping req and res parameters
  • Using app.get instead of app.post
  • Omitting curly braces in arrow function
3. Given this Express code snippet, what will be the response when a POST request with JSON body {"name":"Alice"} is sent to /hello?
const express = require('express');
const app = express();
app.use(express.json());
app.post('/hello', (req, res) => {
  res.send(`Hello, ${req.body.name}!`);
});
medium
A. "Hello, Alice!"
B. "Hello, undefined!"
C. SyntaxError
D. 404 Not Found

Solution

  1. Step 1: Check middleware for JSON parsing

    The code uses app.use(express.json()) which parses JSON bodies into req.body.
  2. Step 2: Understand route handler response

    The handler sends a string using the name property from req.body. Since the body has {"name":"Alice"}, it will respond with "Hello, Alice!".
  3. Final Answer:

    "Hello, Alice!" -> Option A
  4. Quick Check:

    JSON body parsed correctly = "Hello, Alice!" [OK]
Hint: Use express.json() to access req.body data [OK]
Common Mistakes:
  • Forgetting to add express.json() middleware
  • Expecting req.body to be undefined
  • Confusing POST with GET routes
4. What is wrong with this Express POST route code?
app.post('/data', (req, res) => {
  console.log(req.body);
  res.send('Data received');
});
Assuming the client sends JSON data in the request body.
medium
A. Route path should be '/post' instead of '/data'.
B. Callback function parameters are reversed.
C. Missing middleware to parse JSON body.
D. res.send should be called before console.log.

Solution

  1. Step 1: Check for JSON parsing middleware

    The code does not show app.use(express.json()), which is needed to parse JSON request bodies into req.body.
  2. Step 2: Understand effect of missing middleware

    Without JSON parsing middleware, req.body will be undefined, so logging it won't show the sent data.
  3. Final Answer:

    Missing middleware to parse JSON body. -> Option C
  4. Quick Check:

    express.json() middleware required = Missing middleware to parse JSON body. [OK]
Hint: Always add express.json() to parse JSON POST bodies [OK]
Common Mistakes:
  • Assuming req.body works without middleware
  • Swapping req and res parameters
  • Thinking route path name matters for parsing
5. You want to create a POST route /register that accepts JSON data with username and password. It should respond with { success: true, user: username }. Which code correctly implements this?
hard
A. app.post('/register', express.json(), (req, res) => { const { username, password } = req.body; res.json({ success: true, user: username }); });
B. app.post('/register', (req, res) => { const { username, password } = req.body; res.json({ success: true, user: username }); });
C. app.post('/register', (req, res) => { const { username, password } = req.query; res.json({ success: true, user: username }); });
D. app.post('/register', (req, res) => { const { username, password } = req.body; res.send({ success: true, user: username }); });

Solution

  1. Step 1: Ensure JSON body parsing middleware is used

    app.post('/register', express.json(), (req, res) => { const { username, password } = req.body; res.json({ success: true, user: username }); }); uses express.json() as middleware for this route, so req.body will be parsed correctly.
  2. Step 2: Check response format and data source

    It extracts username and password from req.body and responds with JSON using res.json(), which is correct.
  3. Final Answer:

    Option A correctly implements the POST route with JSON parsing and response. -> Option A
  4. Quick Check:

    Use express.json() middleware and res.json() for JSON response [OK]
Hint: Add express.json() middleware and use res.json() to respond [OK]
Common Mistakes:
  • Not using JSON parsing middleware
  • Reading data from req.query instead of req.body
  • Using res.send instead of res.json for JSON response
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard