0
0
Expressframework~10 mins

Application-level middleware 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 create an application-level middleware that logs every request method.

Express
app.use(function(req, res, next) {
  console.log(req.[1]);
  next();
});
Drag options to blanks, or click blank then click option'
Aurl
Bmethod
Cbody
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.url instead of req.method
Trying to log req.body which may not exist yet
Forgetting to call next() to continue middleware chain
2fill in blank
medium

Complete the code to add a middleware that sets a custom header 'X-App-Version' with value '1.0'.

Express
app.use(function(req, res, next) {
  res.setHeader('[1]', '1.0');
  next();
});
Drag options to blanks, or click blank then click option'
AAuthorization
BContent-Type
CX-App-Version
DAccept
Attempts:
3 left
💡 Hint
Common Mistakes
Using standard headers like Content-Type instead of the custom one
Misspelling the header name
Not calling next() to continue middleware
3fill in blank
hard

Fix the error in the middleware that tries to parse JSON body but is missing a function call.

Express
app.use(express.[1]());
Drag options to blanks, or click blank then click option'
Ajson
Burlencoded
Crouter
Dstatic
Attempts:
3 left
💡 Hint
Common Mistakes
Using express.json without parentheses
Using express.urlencoded for JSON data
Using express.static which serves files
4fill in blank
hard

Fill both blanks to create middleware that logs the request URL and then calls next.

Express
app.use(function(req, res, [1]) {
  console.log(req.[2]);
  next();
});
Drag options to blanks, or click blank then click option'
Anext
Bmethod
Curl
Dres
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'res' instead of 'next' as third parameter
Logging req.method instead of req.url
Forgetting to call next()
5fill in blank
hard

Fill all three blanks to create middleware that checks if a user is authenticated and calls next or sends 401.

Express
app.use(function(req, res, next) {
  if (req.[1] && req.[2].isAuthenticated()) {
    [3]();
  } else {
    res.status(401).send('Unauthorized');
  }
});
Drag options to blanks, or click blank then click option'
Auser
Bsession
Cnext
Dbody
Attempts:
3 left
💡 Hint
Common Mistakes
Checking req.session instead of req.user
Calling next without parentheses
Not sending 401 status on failure