Closed sumitsarkar closed 7 years ago
If we use nested routers each with their own set of commonHandlers, the middleware of the deepest router executes first.
it('Should process middlewares in order', function (done) { var v1 = new Router(); var auth = new Router(); var register = new Router(); var first = function (req, res, next) { req.test = [1]; next(); }; var second = function (req, res, next) { req.test.push(2); next(); }; var third = function (req, res, next) { req.test.push(3); next(); }; register.post('/register', function (req, res, next) { res.send({status: 'success', name: req.body.name, commonHandlerInjectedValues: req.test}); return next(); }); auth.use(second, third) auth.add('/auth', register); v1.use(first) v1.add('/v1', auth); v1.applyRoutes(server); request(server) .post('/v1/auth/register') .set('Content-Type', 'application/json') .send({name: 'test'}) .expect(200) .end(function (err, res) { if (err) { return done(err); } res.body.should.deep.equal({status: 'success', name: 'test', commonHandlerInjectedValues: [1,2,3]}); done(); }); });
The test case would fail.
If we use nested routers each with their own set of commonHandlers, the middleware of the deepest router executes first.
The test case would fail.