0
0
Expressframework~20 mins

Why understanding req matters in Express - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Req Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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}!`);
});
AError: req.query is not defined
BHello, Alex!
CHello, !
DHello, undefined!
Attempts:
2 left
💡 Hint
Look at how req.query accesses URL query parameters.
state_output
intermediate
2: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}`);
});
AUser ID is 42
BUser ID is :id
CUser ID is undefined
DUser ID is /user/42
Attempts:
2 left
💡 Hint
Check how route parameters are accessed via req.params.
📝 Syntax
advanced
2: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
});
Aconst user = req.query.username;
Bconst user = req.params.username;
Cconst user = req.body.username;
Dconst user = req.headers.username;
Attempts:
2 left
💡 Hint
Where does Express store parsed JSON data from the request body?
🔧 Debug
advanced
2: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');
});
Areq.body.name is always undefined in POST requests.
BThe route path is incorrect, so req.body is empty.
CThe client did not send a 'name' field in the JSON body.
DThe express.json() middleware is missing, so req.body is undefined.
Attempts:
2 left
💡 Hint
Check if the server can parse JSON bodies.
🧠 Conceptual
expert
3: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?
ABecause req contains user input that must be validated to prevent attacks like injection or XSS.
BBecause req automatically blocks all malicious requests without developer intervention.
CBecause req only holds server configuration data, so it is unrelated to security.
DBecause req is only used for logging and does not affect app behavior.
Attempts:
2 left
💡 Hint
Think about where user data comes from in Express apps.