Qll*_*lli 5 routing node.js express angularjs
使用Angular和Node.js/Express,是否有办法防止直接访问我的部分.html文件,同时仍然允许以下路由处理:
我的Angular路线如下所示:
$stateProvider.state('albums', {
url: '/albums',
templateUrl: '/public/albums',
controller: 'AlbumsCtrl'
});
Run Code Online (Sandbox Code Playgroud)
然后我的快递应用程序执行以下操作
app.get('/public/albums', function(req, res){
res.sendfile('views/partials/albums.html');
});
Run Code Online (Sandbox Code Playgroud)
这一切都正常,但输入"mysite.com/public/albums"可以访问部分.html文件.仍然没有任何东西可以看,因为内容是单独加载的,用户需要为此登录,但我仍然希望以某种方式阻止访问此文件.
在 AngularJS 中发出路径请求/foo/bar与输入 URL 相同domain.com/foo/bar。
您不能阻止其中一个而允许另一个,因为最终它们是对服务器的请求。
然而,您可以做的是使用中间件来阻止未经授权的请求。例如,仅当用户是管理员或仅当用户已登录时。
因此,在您的服务器中您可以编写如下代码:
function ensureAuthenticated (request, response, next) {
//Custom code - If request is authenticated
return next();
//if Not
res.send(403, "Forbidden");
};
app.get('/public/albums', ensureAuthenticated, function (req, res) {
//res.sendfile(filepath);
});
Run Code Online (Sandbox Code Playgroud)