0
0
Expressframework~20 mins

req.method and req.url in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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
intermediate
2:00remaining
What does this Express handler log for a GET request to /home?
Consider this Express.js route handler:
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'); });
A"GET /"
B"POST /home"
C"GET /home"
D"POST /"
Attempts:
2 left
💡 Hint
Remember, req.method is the HTTP method used, and req.url is the path requested.
📝 Syntax
intermediate
1: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?
Aconsole.log(req.method, req.url);
Bconsole.log(req.Method, req.Url);
Cconsole.log(req.method(), req.url());
Dconsole.log(req['method'], req['url']);
Attempts:
2 left
💡 Hint
Check the exact property names and whether they are functions or properties.
state_output
advanced
2: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?
A"/api/items?sort=asc"
B"/api/items"
C"sort=asc"
D"/api/items/?sort=asc"
Attempts:
2 left
💡 Hint
req.url includes the path and the query string exactly as sent.
🔧 Debug
advanced
2:30remaining
Why does this code log 'undefined undefined' for req.method and req.url?
Look at this Express middleware:
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'); });
ABecause req.method and req.url are not valid properties in Express.
BBecause the parameter names are 'request' and 'response', but 'req' is used inside the function.
CBecause response.end() must be called before logging.
DBecause app.use does not receive req and res parameters.
Attempts:
2 left
💡 Hint
Check the variable names used inside the function vs parameters.
🧠 Conceptual
expert
3:00remaining
How does Express determine req.url when using a router mounted at a path?
Suppose you mount a router at /api like this:
app.use('/api', router);

Inside the router, what does req.url represent when a request is made to /api/users?
AIt contains only the query string part, if any.
BIt contains the full original URL, so '/api/users'.
CIt is always '/' inside routers.
DIt contains the path relative to the mount point, so '/users'.
Attempts:
2 left
💡 Hint
Think about how Express routers handle paths relative to where they are mounted.