-
Notifications
You must be signed in to change notification settings - Fork 33
Open
Description
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?
Metadata
Metadata
Assignees
Labels
No labels