Express.static is working great for any view in root directory, but it breaks when I add a subdirectory.
JavaScript
x
2
1
app.use(express.static(path.join(__dirname, 'public')));
2
Works:
JavaScript
1
5
1
//Homepage
2
router.get('/', isAuthenticated, function (req, res, next) {
3
res.render('index.hbs', {user: req.user, message:req.flash('message')}); //add add'l data {title:'Express', addlData:'SomeData', layout:'layout'}
4
});
5
Doesn’t Work:
JavaScript
1
7
1
//Organization Profile
2
router.get('/orgprofile/:username', function(req,res,next){
3
User.findOne({username:req.params.username}, function(err,docs){
4
res.render('orgprofile.hbs',{orgdetails:docs});
5
});
6
});
7
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.
Advertisement
Answer
Use it this way:
JavaScript
1
3
1
app.use('/', express.static(__dirname + '/public'));
2
app.use('/orgprofile', express.static(__dirname + '/public'));
3