0
0
Expressframework~10 mins

req.headers for HTTP headers 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 access HTTP headers in an Express route.

Express
app.get('/info', (req, res) => {
  const headers = req.[1];
  res.send(headers);
});
Drag options to blanks, or click blank then click option'
Aquery
Bbody
Cparams
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.body instead of req.headers
Using req.params or req.query which are for URL parts
2fill in blank
medium

Complete the code to read the 'user-agent' header from the request.

Express
app.get('/agent', (req, res) => {
  const userAgent = req.headers['[1]'];
  res.send(userAgent);
});
Drag options to blanks, or click blank then click option'
Acontent-type
Baccept
Cuser-agent
Dauthorization
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'User-Agent' with uppercase letters (headers keys are lowercase)
Using other header names like 'authorization'
3fill in blank
hard

Fix the error in accessing the 'content-type' header from the request.

Express
app.post('/data', (req, res) => {
  const contentType = req.[1]['content-type'];
  res.send(contentType);
});
Drag options to blanks, or click blank then click option'
Aparams
Bheaders
Cbody
Dquery
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to get headers from req.body or req.params
Using wrong property names
4fill in blank
hard

Fill both blanks to check if the 'authorization' header exists and send a response.

Express
app.get('/auth', (req, res) => {
  if (req.[1]['[2]']) {
    res.send('Authorized');
  } else {
    res.status(401).send('Unauthorized');
  }
});
Drag options to blanks, or click blank then click option'
Aheaders
Bauthorization
Cbody
Dparams
Attempts:
3 left
💡 Hint
Common Mistakes
Checking req.body or req.params instead of req.headers
Using wrong header name spelling
5fill in blank
hard

Fill all three blanks to create a middleware that logs the 'host' header and calls next().

Express
function logHost(req, res, [1]) {
  console.log('Host:', req.[2]['[3]']);
  [1]();
}
Drag options to blanks, or click blank then click option'
Anext
Bheaders
Chost
Dres
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter name instead of next
Accessing headers from wrong object
Forgetting to call next()