Skip to content

subrouters and middlewares #41

Description

@gsabran

It's not clear what is the excepted behavior with middleware at the subrouter level. At the moment, this happens:

const fakeMiddelware = (i) => (req, res, next) => {
  req.MiddelwareCount = req.MiddelwareCount || 0;
  req.MiddelwareCount += i;
  console.log('fakeMiddelware', i, 'run');
  next();
};

Picker.middleware(fakeMiddelware(1));

const group1 = Picker.filter();
group1.middleware(fakeMiddelware(2));

const group2 = Picker.filter();
group2.middleware(fakeMiddelware(4));

Picker.route('/ping', (params, req, res) => {
  console.log('ping received');
  console.log('MiddelwareCount is', req.MiddelwareCount);
  res.end('ok');
});

group1.route('/ping1', (params, req, res) => {
  console.log('ping1 received');
  console.log('MiddelwareCount is', req.MiddelwareCount); // only the first 2 middleware are run, so this is 3
  res.end('ok');
});

group2.route('/pin2', (params, req, res) => {
  console.log('ping2 received');
  console.log('MiddelwareCount is', req.MiddelwareCount); // all three middleware are run, so this is 7
  res.end('ok');
});

Would it not make more sense for the middlewares to be applied only if the route matches, and for a subrouter to apply its parent router's middlewares first and then the subrouter level middlewares?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions