0
0
Expressframework~20 mins

JSON request and response patterns in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSON Mastery in Express
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 route?
Consider this Express route handler that receives a JSON request and sends a JSON response. What will the client receive when sending {"name": "Alice"} in the request body?
Express
app.post('/greet', (req, res) => {
  const { name } = req.body;
  res.json({ message: `Hello, ${name}!` });
});
A{"message":"Hello, Alice!"}
B{"message":"Hello, undefined!"}
CSyntaxError: Unexpected token in JSON
D{"error":"Name not provided"}
Attempts:
2 left
💡 Hint
Think about how Express parses JSON request bodies and how the response is formed.
📝 Syntax
intermediate
1:30remaining
Which option correctly parses JSON request body in Express?
You want to handle JSON data sent by clients in Express. Which code snippet correctly enables JSON parsing middleware?
Aapp.use(express.json());
Bapp.use(bodyParser.urlencoded());
Capp.use(express.text());
Dapp.use(express.raw());
Attempts:
2 left
💡 Hint
Look for the middleware that parses JSON bodies.
🔧 Debug
advanced
2:30remaining
Why does this Express route always send 'undefined' in the response?
Examine the code below. The client sends {"age":30} in the JSON body, but the response is always {"age":undefined}. What is the cause?
Express
app.post('/age', (req, res) => {
  const age = req.body.age;
  res.json({ age: age });
});

// Middleware setup missing
Ares.json() requires a string, not an object
BIncorrect property name; should be req.body.Age
CMissing express.json() middleware to parse JSON body
DRoute method should be app.get instead of app.post
Attempts:
2 left
💡 Hint
Think about how Express reads the request body.
state_output
advanced
2:00remaining
What is the JSON response after multiple requests?
This Express server keeps a count of how many times the '/count' route is called. What JSON does the client receive on the third request?
Express
let count = 0;
app.get('/count', (req, res) => {
  count += 1;
  res.json({ count });
});
A{"count":1}
B{"count":3}
C{"count":0}
D{"count":2}
Attempts:
2 left
💡 Hint
The count variable increases with each request.
🧠 Conceptual
expert
1:30remaining
Which header must be set by the client to send JSON data correctly to an Express server?
When sending JSON data in a POST request to an Express server, which HTTP header should the client include to ensure proper parsing?
AAuthorization: Bearer token
BAccept: application/json
CContent-Length: 0
DContent-Type: application/json
Attempts:
2 left
💡 Hint
This header tells the server the format of the data being sent.