Im using pm2 to handle my nodejs micro services and express-handlebars to handle the views:
var hbs = exphbs.create({
defaultLayout: 'main',
helpers: {
ifeq: function(a, b, options) {
if (a === b) {
return options.fn(this);
}
return options.inverse(this);
},
toJSON : function(object) {
return JSON.stringify(object);
}
}
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
Lunching the app directly (node app.js) works great. But if I lunch it using PM2 (pm2 start app.js) i get:
Error: Failed to lookup view "home" in views directory "/root/views"
When lunching pm2 the current working directory change to /root/ and since my app in not there I got an error from handlebars trying to open the views directory (which is in the app directory).
Is there a way to fix this by telling pm2 the current working directory or by telling the express-handlebars library the complete directory instead of using a relative one?
I am using Koa and SWIG for templates - but I needed to include the path to the views in the app setup:
app.use(koaRender('./server/server-side-views', {
map: { html: 'swig' },
cache: false
}));
I suspect it is more of the same for you. I think in express your code should be something along the line of:
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.set('views', __dirname + '/yourViewDirectory');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.