Complete the code to create a router-level middleware that logs the request method.
const express = require('express'); const router = express.Router(); router.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.
Complete the code to mount the router-level middleware on the '/users' path.
const express = require('express'); const app = express(); const router = express.Router(); app.[1]('/users', router);
The app.use() method mounts middleware or routers on a path.
Fix the error in the router-level middleware to correctly handle errors.
router.use(function(err, req, res, [1]) { console.error(err.stack); res.status(500).send('Something broke!'); });
Error-handling middleware in Express must have four arguments: err, req, res, next. The next argument is required even if not used.
Fill both blanks to create a router-level middleware that only applies to POST requests on '/submit'.
router.[1]('/submit', function(req, res, next) { console.log('POST to /submit'); [2](); });
The router.post method handles POST requests. Calling next() passes control to the next middleware.
Fill all three blanks to create a router-level middleware that logs the request URL, method, and then calls next.
router.use(function(req, res, [1]) { console.log('URL:', req.[2]); console.log('Method:', req.[3]); [1](); });
The middleware function receives req, res, next. The req.url and req.method properties give the URL and HTTP method. Calling next() continues the middleware chain.