Unable to set default directory for views
Hello, I am unable to set default views directory.
OS: Ubuntu 15.04 Node: 4.0.0
I am in app directory (...).
When running node server/server.js, it ignores
app.set('views', path.join(__dirname, 'views')); from server.js and tries to load
.../views/layouts/main.handlebars instead .../server/views/layouts/main.handlebars
.
└── server
├── server.js
└── views
├── home.handlebars
└── layouts
└── main.handlebars
server.js
'use strict';
var path = require('path');
var express = require('express');
var exphbs = require('express-handlebars');
var app = express();
// Rendering engine setup
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
app.set('views', path.join(__dirname, 'views'));
console.log(path.join(__dirname, 'views'));
// Routes
app.get('/', function (req, res) {
res.render('home');
});
app.listen(8888);
EDIT: it works when i am in (.../server/ directory) and node server.js, it is wierd, becouse it should get right path from app.locals.setting.views variable.
@camabeh - path.join(__dirname, 'views')) searches for server/views/<your-file-here>. But the views are not inside server folder. Update the path of views to use the correct path. app.set('views', __dirname + '/../views');
@swarajgiri I don't get it. I think it should respect app.locals.settings.views and set layouts/ and partials/ relative to new views path. It looked in project's root folder for views/partials and views/layouts
Here I fixed it. https://github.com/ericf/express-handlebars/pull/148
@camabeh - I have exactly the same issue. I modified (temporary) the code in the library according to your PR changes and confirm that it solves the problem. Until your PR is merged I choose to set
layoutsDir and partialsDir in engine factory:
app.set('views', config.root + '/server/views');
var exphbs = require('express-handlebars');
app.engine('.hbs', exphbs({
defaultLayout: 'main',
extname: '.hbs',
layoutsDir:'server/views/layouts',
partialsDir:'server/views/partials'
}));
app.set('view engine', '.hbs');
I have a problem: "Error: The partial header could not be found....."
does anyone know how to solve it?
Thanks Bro for this!!