ExpressJS的4.X版本做了很大的更改,ExpressJS4.X变化
其实增加了app.mountpath方法,取代了原来的app.route.
这个方法实际上就是多层级路由模式.借用官方代码简单解释一下:
var admin = express();
admin.get('/', function (req, res) {
console.log(admin.mountpath); // [ '/adm*n', '/manager' ]
res.send('Admin Homepage');
})
var secret = express();
secret.get('/', function (req, res) {
console.log(secret.mountpath); // /secr*t
res.send('Admin Secret');
});
admin.use('/secr*t', secret); // load the 'secret' router on '/secr*t', on the 'admin' sub app
app.use(['/adm*n', '/manager'], admin); // load the 'admin' router on '/adm*n' and '/manager', on the parent app
在这里,创建了两个express对象,先把secret绑定到admin上,再把admin绑定到主app上.
这样就形成了多层级路由分发.