0
0
NestJSframework~10 mins

Why middleware processes requests before handlers in NestJS - Test Your Understanding

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

Complete the code to apply middleware that logs requests before reaching the handler.

NestJS
app.use([1]);
Drag options to blanks, or click blank then click option'
AerrorHandler
BhandleRequest
CresponseMiddleware
DloggerMiddleware
Attempts:
3 left
💡 Hint
Common Mistakes
Using the handler function instead of middleware.
Placing response middleware before request logging.
2fill in blank
medium

Complete the middleware function to call the next handler in the chain.

NestJS
function logger(req, res, [1]) {
  console.log('Request received');
  [1]();
}
Drag options to blanks, or click blank then click option'
Ahandle
Bprocess
Cnext
Dcontinue
Attempts:
3 left
💡 Hint
Common Mistakes
Not calling the next function, causing the request to hang.
Using incorrect parameter names.
3fill in blank
hard

Fix the error in the middleware to ensure it processes requests before handlers.

NestJS
function authMiddleware(req, res, next) {
  if (!req.user) {
    res.status(401).send('Unauthorized');
  } else {
    [1]();
  }
}
Drag options to blanks, or click blank then click option'
Anext
Bhandle
Cprocess
Dcontinue
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to call next() after checks.
Calling a wrong function name.
4fill in blank
hard

Fill both blanks to create middleware that modifies request data before handler.

NestJS
function modifyMiddleware(req, res, [1]) {
  req.customData = 'data';
  [2]();
}
Drag options to blanks, or click blank then click option'
Anext
Bhandle
Cprocess
Dcontinue
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the parameter and call.
Not calling next() after modification.
5fill in blank
hard

Fill all three blanks to create middleware that logs, modifies, and passes request to handler.

NestJS
function combinedMiddleware(req, res, [1]) {
  console.log('Start');
  req.modified = true;
  [2]();
  console.log([3]);
}
Drag options to blanks, or click blank then click option'
Anext
Bhandle
C'End'
Dcontinue
Attempts:
3 left
💡 Hint
Common Mistakes
Not calling next() before logging 'End'.
Using wrong variable names.