Passport.js表示意见
我正在使用护照和一些静态的.html文件。 我想包括他们作为明确的意见或静态文件。 现在我希望像login.html这样的一些.html文件可供所有人使用,但其他视图文件如secret.html应该需要护照。 我知道,你可以为路由使用app.get('somePath', ensureAuthenticated, function (req, res, next) {}
,但我希望它用于静态文件 。
如何做到这一点不hacky的方式?
如果所有“ 安全 ”文件共享一个独特的URL前缀,如/public/secure
,则可以use()
ensureAuthenticated
这个path:
app.use('/public/secure', ensureAuthenticated); app.use('/public', express.static(__dirname + '/public'));
否则, express.static()
中间件不提供许多控制访问的选项。 大多数情况下,它假定所有可用的文件都是“ 公开的” 。
虽然,它通常拒绝发送隐藏文件,遵循* nix标准.
字首:
~/path/to/.secret.html
然后,为了实际服务,您可以使用附加的中间件按照您build议的路线发送文件 :
app.get('/path/to/secret.html', ensureAuthenticated, function (req, res) { res.sendfile(__dirname + '/path/to/.secret.html'); });
而且,您可能需要考虑将其移出static()
目录,