Complete the code to create an application-level middleware that logs every request method.
app.use(function(req, res, next) {
console.log(req.[1]);
next();
});The req.method property contains the HTTP method of the request, such as GET or POST. Logging it helps track request types.
Complete the code to add a middleware that sets a custom header 'X-App-Version' with value '1.0'.
app.use(function(req, res, next) {
res.setHeader('[1]', '1.0');
next();
});The header X-App-Version is a custom header used to indicate the app version. Setting it on the response helps clients know the version.
Fix the error in the middleware that tries to parse JSON body but is missing a function call.
app.use(express.[1]());The express.json() middleware parses incoming JSON requests. It must be called as a function to return the middleware.
Fill both blanks to create middleware that logs the request URL and then calls next.
app.use(function(req, res, [1]) { console.log(req.[2]); next(); });
The third parameter in middleware is next, which you call to continue. The request URL is accessed via req.url.
Fill all three blanks to create middleware that checks if a user is authenticated and calls next or sends 401.
app.use(function(req, res, next) {
if (req.[1] && req.[2].isAuthenticated()) {
[3]();
} else {
res.status(401).send('Unauthorized');
}
});The req.user object holds user info. Calling req.user.isAuthenticated() checks if logged in. If yes, call next() to continue.