Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to log the HTTP method of the request.
Express
app.use((req, res) => {
console.log(req.[1]);
res.end();
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.url instead of req.method
Trying to access req.body without middleware
✗ Incorrect
The req.method property contains the HTTP method (GET, POST, etc.) of the request.
2fill in blank
mediumComplete the code to log the URL path of the request.
Express
app.use((req, res) => {
console.log(req.[1]);
res.end();
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.method instead of req.url
Trying to use req.path which is not always available
✗ Incorrect
The req.url property contains the URL path and query string of the request.
3fill in blank
hardFix the error in the code to correctly check if the request method is POST.
Express
app.use((req, res) => {
if (req.[1] === 'POST') {
res.end('Got a POST request');
} else {
res.end('Not a POST request');
}
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking req.url instead of req.method
Using lowercase 'post' instead of uppercase 'POST'
✗ Incorrect
To check the HTTP method, use req.method. Comparing req.url or others will not work.
4fill in blank
hardFill both blanks to log the method and URL of the request.
Express
app.use((req, res) => {
console.log('Method:', req.[1]);
console.log('URL:', req.[2]);
res.end();
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping method and url properties
Using req.body or req.headers instead
✗ Incorrect
Use req.method for the HTTP method and req.url for the URL path.
5fill in blank
hardFill all three blanks to respond differently based on method and log the URL.
Express
app.use((req, res) => {
if (req.[1] === 'GET') {
res.end('GET request received');
} else if (req.[2] === 'POST') {
res.end('POST request received');
} else {
res.end('Other request');
}
console.log('URL:', req.[3]);
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.url in the if conditions
Logging req.method instead of req.url
✗ Incorrect
Check req.method for the HTTP method in both conditions and log req.url for the URL.