Skip to content

Instantly share code, notes, and snippets.

@mjm mjm/router_test.js
Created Sep 22, 2015

Embed
What would you like to do?
Independent routers don't isolate their middleware
var express = require('express');
var app = express();
var router1 = express.Router();
router1.use(function (req, res, next) {
console.log('router1 middleware called.');
next();
});
router1.get('/1', function (req, res) {
res.send('1');
});
var router2 = express.Router();
router2.use(function (req, res, next) {
console.log('router2 middleware called.');
next();
});
router2.get('/2', function (req, res) {
res.send('2');
});
app.use(router1);
app.use(router2);
app.listen(4000);
@mjm

This comment has been minimized.

Copy link
Owner Author

mjm commented Sep 22, 2015

GET / prints both log statements.
GET /1 prints just the router1 statement.
GET /2 prints both log statements.

@knightmahajan

This comment has been minimized.

Copy link

knightmahajan commented Dec 11, 2017

Can we add 2 or more middlewares on the same route ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.