Skip to content

Instantly share code, notes, and snippets.

@mjm
Created September 22, 2015 00:26
Show Gist options
  • Save mjm/497a23f68826f8ca473b to your computer and use it in GitHub Desktop.
Save mjm/497a23f68826f8ca473b to your computer and use it in GitHub Desktop.
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
Copy link
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
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