angularjs表达路由错误

时间:2014-12-21 06:32:37

标签: javascript angularjs express url-routing

在使用两个参数路由路由时遇到一个奇怪的问题,我似乎无法对其进行调试并找到错误发生的位置。虽然我之前也使用过以下代码但由于某种原因它现在无法正常工作。

我有两个角度应用,一个在url '/'上运行,另一个在url '/dashboard'

现在,我的应用A在网址' /'没有这样的问题,但是当我登录并使用此配置移动到应用程序B时 -

angular.module('student').config(function($routeProvider,$locationProvider){
    $locationProvider.html5Mode({
        enabled: true,
        requireBase: false
    });
    $routeProvider
        .when('/dashboard', { templateUrl: '/partials/dashboard/student/index',
            controller: 'StudentDashBoardController'
        })
        .when('/profile', { templateUrl: '/partials/dashboard/student/profile',
            controller: 'ProfileController'
        })
        .when('/myfeeds', { templateUrl: '/partials/dashboard/student/myfeeds',
            controller: 'MyFeedsController'
        })
        .when('/student/test', { templateUrl: '/partials/dashboard/student/user',
            controller: 'UserController'
        });
});

并且express中的路由配置是 -

app.get('/dashboard',function(req,res){
        if(!req.isAuthenticated()) {
            res.redirect('/403');
        } else if(req.user.roles.indexOf("student") != -1){
            res.render('student');
        } else if(req.user.roles.indexOf("teacher") != -1){
            res.render('teacher');
        } else if(req.user.roles.indexOf("publisher") != -1){
            res.render('publisher');
        }
    });
app.get('/profile',auth.requiresApiLogin,users.renderStudentUser);
    app.get('/student/test',auth.requiresApiLogin,users.renderStudentUser);
    app.get('/myfeeds',auth.requiresApiLogin,users.renderStudentUser);
app.get('/partials/*',function(req,res){
        res.render('../../public/app/' + req.params['0']);
    });
app.get('*',function(req,res){
        res.render('index',{
            bootstrappedUser: req.user
        });
    });

现在,函数users.renderStudentUser就像 -

exports.renderStudentUser = function(req,res){
    User.findOne({username:req.user.username}).exec(function(err,user){
        if(err){
            res.status(400);
            return res.send({reason:err.toString()});
        }
        res.render('student',{
            user:user
        });
    });
};

所以,基本上它将用户路由到应用B. 我正在使用它,因为如果用户直接在浏览器中点击'/profile',那么app.get('*')路由会将其重定向到应用A,在' /'所以我已经为'/profile','/myfeeds','/student/test'明确设置了三条路由,将它们重定向到app B,其中angularjs路由器将处理每条路由。 现在,当我试图击中问题时 '/student/test'直接在浏览器中,我收到所有脚本的404错误消息,甚至是部分文件,但呈现的视图仅为student,因为在该视图中定义的脚本显示为404在控制台中的状态。 令人惊讶的是,当我试图点击' / dashboard'它工作,然后如果我去' / student / test'通过像

这样的超链接
<a href="/student/test"> xyz </a>

它可以正常加载部分文件。

现在,为了调试,我将expressjs和angularjs路线从'/student/test'更改为&#39; / student&#39;它可以通过直接在浏览器中使用此URL或使用超链接来实现两种方式。

我还试图重命名&#39; / student / test&#39;到'/xyz/test'以验证我的expressjs路线中没有多条匹配此路线。

现在有人可以建议我犯了错误,或者我应该提供哪些更多信息来诊断这个错误,或者我应该如何调试它以找到错误。

0 个答案:

没有答案
相关问题