Before, after and around

The before, after and around methods can easily be replaced by middleware:

Old:

  1. let start;
  2. ctrl.before(function (req, res) {
  3. start = Date.now();
  4. });
  5. ctrl.after(function (req, res) {
  6. console.log('Request handled in ', (Date.now() - start), 'ms');
  7. });

New:

  1. router.use(function (req, res, next) {
  2. let start = Date.now();
  3. next();
  4. console.log('Request handled in ', (Date.now() - start), 'ms');
  5. });

Note that unlike around middleware receives the next function as the third argument (the “opts” argument has no equivalent).