Angularjs,Expressjs和passportjs用于服务器端的身份validation检查

我有一个Node.js应用程序,我已经使用了Yeoman脚手架Angular。 我也使用ExpressJS服务器端。

所以基本上我的app结构是:

app -->views --> more files server.js 

结构比这更重,但这是为了保持简单。

基本上我在服务器端使用PassportJS进行身份validation。 路由目前正在通过AngularJS路由参数进行。

我现在遇到了一个问题,因为我需要使用中间件方法进行身份validation:

 function ensureAuthenticated(req, res, next) { console.log('authenticate=' + req.isAuthenticated()); if (req.isAuthenticated()) { return next(); } res.redirect('/') } 

我在服务器端创build了一个app.get来检查用户是否试图进入pipe理页面而不login:

 app.get('/admin', ensureAuthenticated, function(req, res){ console.log('get admin' + req.params); res.sendfile(__dirname + '/app/views/admin/users.html'); }); 

但它从来没有去的方法。 我究竟做错了什么?

任何build议将是伟大的。

 if (!req.isAuthenticated()) { return res.send(401, 'User is not authorized'); } next(); 

它不工作的原因是因为在客户端configuration路由时,angularjs locationprovider未设置为htmlmode5。 添加了以下行:

 $locationProvider.html5Mode(true); 

这基本上从url中删除了“#”。