Using express.static middleware in an authorized route

后端 未结 2 1700
时光取名叫无心
时光取名叫无心 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: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);
    });
    

提交回复
热议问题