Using express.static middleware in an authorized route

后端 未结 2 1691
时光取名叫无心
时光取名叫无心 2020-12-14 11:35

I\'m using node with express and passportjs to restrict access to files located in a private folder. I have reduced my code to the following. Everything in the public static

相关标签:
2条回答
  • 2020-12-14 12:12

    You can also add express.static(__dirname + '/private'); to your app.config.

    app.configure(function() {
      app.use(app.router);
      app.use(express.logger('dev')); 
      app.use('/public',express.static(__dirname + '/public'));
      app.use('/private',express.static(__dirname + '/private'));
    });
    

    The private path middleware would be executed anytime a path began with private.

    0 讨论(0)
  • 2020-12-14 12:24

    sheesh staring at me the whole time

    app.get('/private/:file', function(req, res, next){
        console.log('about to send restricted file '+ req.params.file);
        req.url = req.url.replace(/^\/private/, '')
        staticMiddleware(req, res, next);
    });
    

    Edit 11-29-2014

    So after someone posted to the question I came back to this answer to find that even though I mention passportjs I never showed how I ended up using this function.

    var staticMiddlewarePrivate = express['static'](__dirname + '/private');
    
    app.get('/private/*/:file', auth.ensureAuthenticated, function(req, res, next){
        console.log('**** Private ****');
        req.url = req.url.replace(/^\/private/, '');
        staticMiddlewarePrivate(req, res, next);
    });
    
    0 讨论(0)
提交回复
热议问题