javascript - Authenticate before serving up directory in Express -
javascript - Authenticate before serving up directory in Express -
i having problem trying authenticate users before view express directory file tree. can authenticate on other pages not on "/dat/:file(*)" when pass authentication route before downloading file. when user goes '/', express redirect them if not logged in. but, if user goes '/dat', express not authenticate , allow them browse file tree. i'm using express@3.4.8 , help great. thanks!
app.configure( function() { app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.use('/', express.static(__dirname + '/public')); app.use('/dat', express.directory(__dirname + '/public/dat', {hidden: true, icons: true})); app.use(express.json()); app.use(express.urlencoded()); app.use(express.methodoverride()); app.use(express.cookieparser('secret')); app.use(express.session({ secret: 'secret', maxage: 3600000 })); app.use(passport.initialize()); app.use(passport.session()); app.use(app.router); }); app.get('/', ensure_authenticated, routes.index); app.get('/dat/:file(*)', ensure_authenticated, function(req, res, next) { var path = __dirname + '/' + req.params.file; res.download(path); });
the order of middleware matters.
app.use('/dat', express.directory(__dirname + '/public/dat', {hidden: true, icons: true}));
is before:
app.get('/dat/:file(*)', ensure_authenticated, function(req, res, next) { var path = __dirname + '/' + req.params.file; res.download(path); });
as result, first middleware handling request.
moving first route after app.use(app.router))
should prepare this.
you'll want add together ensure_authenticate
route express.directory
if want user authenticated see directory listing well.
app.use('/dat', ensure_authenticate, express.directory ...
javascript node.js authentication express routes
Comments
Post a Comment