express4.x 中一个路由中间件可以挂载到多个路由上,一个路由也可以绑定多个路由中间件,如:
1 //多个路由匹配一个路由中间件 2 app.use(['/gre+t', '/hel{2}o'], greet); 3 4 var greet = express.Router(); 5 6 greet.get('/jp', function (req, res) { 7 console.log(req.baseUrl); // /greet 8 res.send('Konichiwa!'); 9 }); 10 11 app.use('/greet', greet); 12 13 //多个中间件匹配一个路由 14 app.use('/rtest', [r1, r2]); 15 16 var r1 = express.Router(); 17 r1.get('/', function (req, res, next) { 18 console.log("r1 router"); 19 next(); 20 }) 21 22 var r2 = express.Router(); 23 r2.get('/', function (req, res, next) { 24 console.log('r2 router '); 25 res.send("r2 router " + req.app.get("view engine")) 26 // next(); 27 })