Challenge - 5 Problems
Express req.method & req.url Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Express handler log for a GET request to /home?
Consider this Express.js route handler:
What will be logged if a client sends a GET request to
app.use((req, res) => { console.log(req.method, req.url); res.end('done'); });What will be logged if a client sends a GET request to
/home?Express
app.use((req, res) => { console.log(req.method, req.url); res.end('done'); });Attempts:
2 left
💡 Hint
Remember, req.method is the HTTP method used, and req.url is the path requested.
✗ Incorrect
The req.method property holds the HTTP method, here 'GET'. The req.url property holds the path, here '/home'. So the console logs "GET /home".
📝 Syntax
intermediate1:30remaining
Which option correctly accesses the HTTP method and URL in Express?
You want to log the HTTP method and URL of a request in Express. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Check the exact property names and whether they are functions or properties.
✗ Incorrect
In Express, req.method and req.url are properties, not functions, and are lowercase. Option A uses correct casing and syntax.
❓ state_output
advanced2:00remaining
What is the value of req.url after a request to /api/items?sort=asc
If a client sends a GET request to
/api/items?sort=asc, what will req.url contain in Express?Attempts:
2 left
💡 Hint
req.url includes the path and the query string exactly as sent.
✗ Incorrect
req.url contains the full path and query string, so it includes '?sort=asc'.
🔧 Debug
advanced2:30remaining
Why does this code log 'undefined undefined' for req.method and req.url?
Look at this Express middleware:
Why does it log 'undefined undefined'?
app.use((request, response) => { console.log(req.method, req.url); response.end('ok'); });Why does it log 'undefined undefined'?
Express
app.use((request, response) => { console.log(req.method, req.url); response.end('ok'); });Attempts:
2 left
💡 Hint
Check the variable names used inside the function vs parameters.
✗ Incorrect
The function parameters are named 'request' and 'response', but the code uses 'req' which is undefined, so req.method and req.url are undefined.
🧠 Conceptual
expert3:00remaining
How does Express determine req.url when using a router mounted at a path?
Suppose you mount a router at
Inside the router, what does
/api like this:app.use('/api', router);Inside the router, what does
req.url represent when a request is made to /api/users?Attempts:
2 left
💡 Hint
Think about how Express routers handle paths relative to where they are mounted.
✗ Incorrect
Inside a router mounted at '/api', req.url is the path relative to '/api', so for '/api/users' it is '/users'.