考虑一个使用 angular.js 路由的应用程序。假设有人想要将路由 /this
和 /that
委托(delegate)给 Angular,并且这些在应用程序中的编码类似于:
function($routeProvider) {
$routeProvider
.when('/this', { //..})
.when('/that', { //::})
.otherwise({
redirectTo: '/this'
});
如果使用 HTML5 模式,则必须配置服务器端路由才能委托(delegate)给 Angular。我现在拥有的是:
app.get('/*', function(req,res){
res.render('home.ejs');
});
现在,我想要一些路由,例如 /those
,由服务器直接处理。在当前配置中,/those
被/*
拦截。我知道一个可能的解决方案是枚举服务器中的所有 Angular 路线,即:
app.get('/this', function(req,res){
res.render('home.ejs');
});
app.get('/that', function(req,res){
res.render('home.ejs');
});
这可以工作,但是丑陋且不稳定,因为很容易忘记某些内容并产生错误。
问题:
我想知道是否有办法将 /*
留在服务器端路由器中,并指定这不应包含 /those
,为此要实现特定的 app.get('/those',..)
。
最佳答案
解决方案是配置 Express 服务器路由,类似于:
app.get('/those',..);
...
app.get('*', function(req,res){
res.render('home.ejs');
});
其中路由'*'
位于路由文件的最末尾。这样 '*'
仅拦截文件前面未明确指定的路由。
关于javascript - 如何从 Angular 路由中排除路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26764686/