Challenge - 5 Problems
Express Req Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Express route output when receiving a GET request with query ?name=Alex?
Consider this Express route handler. What will the response be if the client sends a GET request to /hello?name=Alex?
Express
app.get('/hello', (req, res) => { res.send(`Hello, ${req.query.name}!`); });
Attempts:
2 left
💡 Hint
Look at how req.query accesses URL query parameters.
✗ Incorrect
The req.query object contains the parsed query string parameters. Here, req.query.name is 'Alex', so the response is 'Hello, Alex!'.
❓ state_output
intermediate2:00remaining
What is the value of req.params.id in this route for URL /user/42?
Given this Express route, what will be the value of req.params.id when a client requests /user/42?
Express
app.get('/user/:id', (req, res) => { res.send(`User ID is ${req.params.id}`); });
Attempts:
2 left
💡 Hint
Check how route parameters are accessed via req.params.
✗ Incorrect
The :id in the route path captures the segment after /user/. For /user/42, req.params.id is '42'.
📝 Syntax
advanced2:00remaining
Which option correctly accesses JSON data sent in a POST request body?
Assuming Express is set up with express.json() middleware, which code correctly reads the 'username' field from the request body?
Express
app.post('/login', (req, res) => {
// Access username here
});Attempts:
2 left
💡 Hint
Where does Express store parsed JSON data from the request body?
✗ Incorrect
The req.body object contains parsed data from the request body when using express.json() middleware. req.params and req.query are for URL parameters and query strings, headers are different.
🔧 Debug
advanced2:00remaining
Why does this Express route always respond with 'undefined' for req.body.name?
Look at this code snippet. Why does req.body.name print 'undefined' even when the client sends JSON with a 'name' field?
Express
app.post('/submit', (req, res) => { console.log(req.body.name); res.send('Received'); });
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 remains undefined.
🧠 Conceptual
expert3:00remaining
Why is understanding the req object crucial for building secure Express apps?
Which statement best explains why knowing the structure and content of the req object is important for security?
Attempts:
2 left
💡 Hint
Think about where user data comes from in Express apps.
✗ Incorrect
The req object holds all data sent by the client, including user input. Validating and sanitizing this data is essential to protect the app from security threats.