Express.js条件router.all()

我不明白为什么从2016年开始的url不起作用。 它只是永远加载。

主要(/) ,pipe理员(/admin/*)/home工作没有问题。

 function stringStartsWith(string, prefix) { return string.slice(0, prefix.length) == prefix; } router.get('/', csrfProtection, indexOnly, indexController.index); router.get('/admin', adminOnly, adminController.index); router.all('/*', function(req, res, next) { if (req.originalUrl == '/home') { next(); } else if (stringStartsWith(req.originalUrl, "/admin")) { router.all('/admin/*', function(req, res, next) { if (req.originalUrl == '/admin') { next(); // it doesn't do anything, just allows the route above to work (admin welcome page.) } else { res.sendFile(path.resolve('views/backend/index.html')); } }); } else if (stringStartsWith(req.originalUrl, "/2016")) { router.all('/2016/*', function(req, res, next) { res.sendFile(path.resolve('views/frontend/index/index.html')); }); } else { res.sendFile(path.resolve('views/frontend/index.html')); } }); 

为什么你把2016路线放在其他路线里面? 它应该简单地像其他的路线:

 function stringStartsWith(string, prefix) { return string.slice(0, prefix.length) == prefix; } router.get('/', csrfProtection, indexOnly, indexController.index); router.get('/admin', adminOnly, adminController.index); router.all('/2016/*', function(req, res, next) { res.sendFile(path.resolve('views/frontend/index/index.html')); }); router.all('/*', function(req, res, next) { if (req.originalUrl == '/home') { next(); } else if (stringStartsWith(req.originalUrl, "/admin")) { router.all('/admin/*', function(req, res, next) { if (req.originalUrl == '/admin') { next(); // it doesn't do anything, just allows the route above to work (admin welcome page.) } else { res.sendFile(path.resolve('views/backend/index.html')); } }); } else { res.sendFile(path.resolve('views/frontend/index.html')); } });