0
0
Expressframework~10 mins

Middleware execution flow (req, res, next) 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 define a middleware function that logs the request method.

Express
app.use(function(req, res, [1]) {
  console.log(req.method);
  next();
});
Drag options to blanks, or click blank then click option'
Asend
Bnext
Cend
Dhandle
Attempts:
3 left
💡 Hint
Common Mistakes
Using send or end instead of next causes the request to stop.
2fill in blank
medium

Complete the code to call the next middleware after logging the URL.

Express
app.use((req, res, [1]) => {
  console.log(req.url);
  [1]();
});
Drag options to blanks, or click blank then click option'
Ahandle
Bsend
Cend
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to call next() causes the request to hang.
3fill in blank
hard

Fix the error in the middleware to properly pass control to the next function.

Express
app.use(function(req, res, next) {
  console.log('Middleware running');
  [1]();
});
Drag options to blanks, or click blank then click option'
Anext
Bsend
Cend
Dhandle
Attempts:
3 left
💡 Hint
Common Mistakes
Writing next; instead of next(); does not call the function.
4fill in blank
hard

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

Express
app.use(function(req, res, [1]) {
  if (!req.user) {
    res.status(401).[2]('Unauthorized');
  } else {
    next();
  }
});
Drag options to blanks, or click blank then click option'
Anext
Bsend
Cjson
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Using end or json instead of send for the response.
5fill in blank
hard

Fill all three blanks to create middleware that logs method, URL, and calls next.

Express
app.use(function(req, res, [1]) {
  console.log(req.[2] + ' ' + req.[3]);
  next();
});
Drag options to blanks, or click blank then click option'
Anext
Bmethod
Curl
Dbody
Attempts:
3 left
💡 Hint
Common Mistakes
Using body instead of url for the request path.