0
0
Expressframework~10 mins

Router level middleware 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 create a router-level middleware that logs the request method.

Express
const express = require('express');
const router = express.Router();

router.use(function(req, res, next) {
  console.log(req.[1]);
  next();
});
Drag options to blanks, or click blank then click option'
Apath
Bmethod
Curl
Dbody
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.url instead of req.method
Trying to log req.body in middleware without parsing
2fill in blank
medium

Complete the code to mount the router-level middleware on the '/users' path.

Express
const express = require('express');
const app = express();
const router = express.Router();

app.[1]('/users', router);
Drag options to blanks, or click blank then click option'
Ause
Bget
Cpost
Dlisten
Attempts:
3 left
💡 Hint
Common Mistakes
Using app.get instead of app.use
Trying to use app.listen to mount middleware
3fill in blank
hard

Fix the error in the router-level middleware to correctly handle errors.

Express
router.use(function(err, req, res, [1]) {
  console.error(err.stack);
  res.status(500).send('Something broke!');
});
Drag options to blanks, or click blank then click option'
Anext
Bdone
Ccallback
Dhandle
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the fourth argument
Naming the fourth argument incorrectly
4fill in blank
hard

Fill both blanks to create a router-level middleware that only applies to POST requests on '/submit'.

Express
router.[1]('/submit', function(req, res, next) {
  console.log('POST to /submit');
  [2]();
});
Drag options to blanks, or click blank then click option'
Apost
Bget
Cnext
Dsend
Attempts:
3 left
💡 Hint
Common Mistakes
Using router.get instead of router.post
Calling res.send instead of next()
5fill in blank
hard

Fill all three blanks to create a router-level middleware that logs the request URL, method, and then calls next.

Express
router.use(function(req, res, [1]) {
  console.log('URL:', req.[2]);
  console.log('Method:', req.[3]);
  [1]();
});
Drag options to blanks, or click blank then click option'
Anext
Burl
Cmethod
Dsend
Attempts:
3 left
💡 Hint
Common Mistakes
Using res.send instead of next
Mixing up url and method properties