我有一个基于 express 的项目,需要基于 Passport 的身份验证。
后台是一个用作静态文件的 angularjs 应用程序。
我的验证码完全基于https://github.com/jaredhanson/passport-local/blob/master/examples/express3-no-connect-flash/app.js
如果您未通过身份验证,请不要为 Angular 应用程序提供服务。我尝试在/admin 路由上添加 ensureAuthenticated 但它使路由不起作用(404)。一旦我删除了 ensureAuthenticated,就会提供/admin。
app.use(express.static(path.join(__dirname, 'public')));
app.use('/admin', ensureAuthenticated, express.static(path.join(__dirname, 'admin')));
//serve routes
app.use(app.router);
公用文件夹包含登录页面。
我怎样才能做到这一点?
最佳答案
遇到了同样的问题,这就是我最终要做的!
app.use doesn't let you chain middlewares in that way. The various app.VERB functions do, but app.use doesn't. That's for one middleware at a time.
If you split the 2 middlewares out into separate calls, you should get the results you want:
app.use('/admin', ensureAuthenticated);
app.use('/admin', express.static(path.join(__dirname, 'admin')));
Cannot use basic authentication while serving static files using express
关于node.js - 如何用 Passport 保护 express 中的静态文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21335868/