Created
September 22, 2015 00:26
-
-
Save mjm/497a23f68826f8ca473b to your computer and use it in GitHub Desktop.
Independent routers don't isolate their middleware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
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
GET / prints both log statements.
GET /1 prints just the router1 statement.
GET /2 prints both log statements.