协调Express(Passport)和AngularJS路线

我正在构build一个MEAN栈应用程序,最后到达创build用户身份validation的地步。 为此,我按照这个教程: http : //code.tutsplus.com/tutorials/authenticating-nodejs-applications-with-passport–cms-21619

现在,当我将其纳入我的项目时,它的工作,但只是部分。 也就是说,我可以正确浏览的唯一页面就是应用程序的主页。 如果我点击任何链接或在地址栏中键入除家以外的内容,则会将我带回login屏幕。

有什么可能的原因呢?

我的routes / index.js文件如下所示:

var express = require('express'); var router = express.Router(); var isAuthenticated = function (req, res, next) { // if user is authenticated in the session, call the next() to call the next request handler // Passport adds this method to request object. A middleware is allowed to add properties to // request and response objects if (req.isAuthenticated()) return next(); // if the user is not authenticated then redirect him to the login page res.redirect('/'); } module.exports = function(passport){ /* GET login page. */ router.get('/', function(req, res) { // Display the Login page with any flash message, if any res.render('index', { message: req.flash('message') }); }); /* Handle Login POST */ router.post('/login', passport.authenticate('login', { successRedirect: '/home', failureRedirect: '/', failureFlash : true })); /* GET Registration Page */ router.get('/signup', function(req, res){ res.render('register',{message: req.flash('message')}); }); /* Handle Registration POST */ router.post('/signup', passport.authenticate('signup', { successRedirect: '/home', failureRedirect: '/signup', failureFlash : true })); /* GET Home Page */ router.get('/home', isAuthenticated, function(req, res){ res.render('home', { user: req.user }); }); /* Handle Logout */ router.get('/signout', function(req, res) { req.logout(); res.redirect('/'); }); return router; } 

我也有一些AngularJS路由在另一个文件中指定(在我开始添加身份validation之前,应用程序完全可以正常使用这些path)。

 app.config(['$routeProvider', function($routeProvider){ $routeProvider .when('/', { templateUrl: 'partials/home.html', controller: 'HomeCtrl' }) .when('/calendar',{ templateUrl: 'partials/calendar.html', //controller: 'Calendar' }) .when('/add-activity', { templateUrl: 'partials/activity-form.html', controller: 'AddActivityCtrl' }) .when('/activity/:id',{ templateUrl: 'partials/activity-form.html', controller: 'EditActivityCtrl' }) .when('/activity/delete/:id', { templateUrl: 'partials/activity-delete.html', controller: 'DeleteActivityCtrl' }) .otherwise({ redirectTo: '/' }); }]); 

有什么我失踪?

PS我注意到,目前我的主页的URL是

HTTP://本地主机:3000 /家居#/

而之前它是

HTTP://本地主机:3000 /#/

我添加了“home”以区别于身份validation页面“/”; 然而,我不确定“#”是如何被加到第一个引号的path上的。

我能够解决这个如下。 我改变了Express路由包含一个

“login”

路线和改变主线只是

“/”

结果,家路成了

HTTP://本地主机:3000 /#/

散列符号是由Angular和Angular添加的。 从我的理解来看,Angular把这样的path看作是“/”。 然后,其余的路由是由Angular完成的,我有一个单页的AngularJS应用程序。

工作代码:Express

 var express = require('express'); var router = express.Router(); module.exports = function(passport){ var isAuthenticated = function (req, res, next) { // if user is authenticated in the session, call the next() to call the next request handler // Passport adds this method to request object. A middleware is allowed to add properties to // request and response objects if (req.isAuthenticated()){ //console.log(next()); return next(); } // if the user is not authenticated then redirect him to the login page res.redirect('/login'); } /* GET login page. */ router.get('/login', function(req, res) { // Display the Login page with any flash message, if any res.render('login', { message: req.flash('message') }); }); /* Handle Login POST */ router.post('/login', passport.authenticate('login', { successRedirect: '/', failureRedirect: '/login', failureFlash : true })); /* GET Registration Page */ router.get('/signup', function(req, res){ res.render('register',{message: req.flash('message')}); }); /* Handle Registration POST */ router.post('/signup', passport.authenticate('signup', { successRedirect: '/', failureRedirect: '/signup', failureFlash : true })); /* GET Home Page when logged in */ router.get('/', isAuthenticated, function(req, res){ res.render('index', { user: req.user }); }); /* GET Home Page */ router.get('/', isAuthenticated, function(req, res){ res.render('index', { user: req.user }); }); /* Handle Logout */ router.get('/signout', function(req, res) { req.logout(); res.redirect('/login'); }); return router; } 

工作代码:Angular

 app.config(['$routeProvider', function($routeProvider){ $routeProvider .when('/', { templateUrl: 'partials/home.html', controller: 'HomeCtrl' }) .when('/calendar',{ templateUrl: 'partials/calendar.html', //controller: 'Calendar' }) .when('/add-activity', { templateUrl: 'partials/activity-form.html', controller: 'AddActivityCtrl' }) .when('/activity/:id',{ templateUrl: 'partials/activity-form.html', controller: 'EditActivityCtrl' }) .when('/activity/delete/:id', { templateUrl: 'partials/activity-delete.html', controller: 'DeleteActivityCtrl' }) .otherwise({ redirectTo: '/' }); }]);