如何从Angular路由中排除路由?

时间:2014-11-05 18:42:56

标签: javascript angularjs node.js express routes

考虑使用angular.js路由的应用程序。假设有人想要将路由/this/that委托给angular,并且这些路由在应用中的编码类似于:

function($routeProvider) {
    $routeProvider
      .when('/this', { //..})
      .when('/that', { //::})
      .otherwise({
          redirectTo: '/this'
      });

如果使用HTML5模式,则必须配置服务器端路由才能委派给angular。我现在拥有的是:

app.get('/*', function(req,res){
    res.render('home.ejs');
    });

现在,我希望有一些路由,例如/those,由服务器直接处理。在当前配置中,/those拦截了/*。我知道一个可能的解决方案是枚举服务器中的所有角度路由,即:

app.get('/this', function(req,res){
        res.render('home.ejs');
        });
app.get('/that', function(req,res){
        res.render('home.ejs');
        });

这很有效,但是很难看,因为很容易忘记并产生错误。

问题: 我想知道是否有办法将/*留在服务器端路由器中,并指定这不应包含/those,为此要实现特定的app.get('/those',..)

1 个答案:

答案 0 :(得分:0)

解决方案是将Express服务器路由配置为:

app.get('/those',..);
...
app.get('*', function(req,res){
    res.render('home.ejs');
    });

其中路由'*'位于路径文件的最后。这种方式'*'仅拦截文件中先前未明确指定的路由。