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.
__dirname
is not changed per request. To resolve this, please show the URL you wish to be served as a static request and the directory structure where you want the static request to come from. And, also,express.static()
has nothing to do withres.render()
calls if that is what your problem is. Those are normal file operations that go through the template layout engine, not through the middleware code path whereexpress.static()
is. – Sensual