Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
61
rated 0 times [  68] [ 7]  / answers: 1 / hits: 15174  / 9 Years ago, mon, january 4, 2016, 12:00:00

Express.static is working great for any view in root directory, but it breaks when I add a subdirectory.



app.use(express.static(path.join(__dirname, 'public')));


Works:



//Homepage
router.get('/', isAuthenticated, function (req, res, next) {
res.render('index.hbs', {user: req.user, message:req.flash('message')}); //add add'l data {title:'Express', addlData:'SomeData', layout:'layout'}
});


Doesn't Work:



//Organization Profile
router.get('/orgprofile/:username', function(req,res,next){
User.findOne({username:req.params.username}, function(err,docs){
res.render('orgprofile.hbs',{orgdetails:docs});
});
});


I'm guessing __dirname is changed to whatever directory the get request is made from, but I just want to use the root (/public) directory for all static requests.


More From » node.js

 Answers
52

Use it this way:



app.use('/', express.static(__dirname + '/public'));
app.use('/orgprofile', express.static(__dirname + '/public'));

[#63845] Saturday, January 2, 2016, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
meadowe

Total Points: 0
Total Questions: 97
Total Answers: 97

Location: Laos
Member since Fri, Sep 11, 2020
4 Years ago
;