只允许login用户访问html页面

我有一个公共文件夹中的app.html正在提供静态文件来expression。

我使用了这里的代码。

但不是从视图文件夹的profile.ejs (按照链接中的代码),我想从公共文件夹内提供app.html

  app.get('/app.html', isLoggedIn, function(req, res) { res.render('app.html'/*, { user : req.user // get the user out of session and pass to template }*/); console.log("APP.HTML"); }); function isLoggedIn(req, res, next) { // if user is authenticated in the session, carry on if (req.isAuthenticated()) return next(); // if they aren't redirect them to the home page res.redirect('/'); } 

但它并不限制login用户的访问权限,我也可以在注销后直接访问它。

这里有什么可能是错的?

问题很可能是你的app.get()之前express.static() 。 静态中间件检查文件是否存在,如果存在则发送。 所以你需要把这个app.get()放在你的express.static()中间件之前。