Complete the code to define a middleware function that logs the request method.
function logger(req, res, [1]) {
console.log(req.method);
next();
}The third parameter in middleware functions is always called next. It is used to pass control to the next middleware.
Complete the code to use the middleware in an Express app.
const express = require('express'); const app = express(); app.use([1]); app.listen(3000);
When passing middleware to app.use, you pass the function itself without calling it.
Fix the error in the middleware to ensure it passes control correctly.
function checkAuth(req, res, [1]) { if (!req.user) { res.status(401).send('Unauthorized'); } else { next(); } // Missing call to continue middleware chain }
next() causing the request to hang.The middleware must call next() to continue to the next middleware or route handler.
Fill both blanks to create middleware that modifies the request and passes control.
function addRequestTime(req, res, [1]) { req.requestTime = Date.now(); [2](); }
res.send instead of next().The middleware function receives next as the third parameter and calls next() to continue.
Fill all three blanks to create middleware that checks a header and either continues or sends an error.
function checkHeader(req, res, [1]) { if (req.headers['x-api-key'] === '[2]') { [3](); } else { res.status(403).send('Forbidden'); } }
next() or using wrong parameter names.The middleware uses next as the third parameter and calls next() to continue. It checks if the header matches '12345'.