0
0
Expressframework~10 mins

req.method and req.url in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Amethod
Burl
Cbody
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.url instead of req.method
Trying to access req.body without middleware
2fill in blank
medium

Complete 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'
Apath
Bmethod
Curl
Dquery
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.method instead of req.url
Trying to use req.path which is not always available
3fill in blank
hard

Fix 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'
Aheaders
Bbody
Curl
Dmethod
Attempts:
3 left
💡 Hint
Common Mistakes
Checking req.url instead of req.method
Using lowercase 'post' instead of uppercase 'POST'
4fill in blank
hard

Fill 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'
Amethod
Burl
Cbody
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping method and url properties
Using req.body or req.headers instead
5fill in blank
hard

Fill 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'
Aurl
Bmethod
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.url in the if conditions
Logging req.method instead of req.url