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.
This is my nodejs express
setup for view and directories using handlebars:
- server.js
- routes
|-- index.js
-config
|-- config.js
- client
|-- public
| | -- js
| | -- css
| | -- font
|-- views
|-- index.html
|-- layout
|-- layout.handlebars
| ... other directories
And here is my node.js code:
// Set static folder
app.use[express.static[path.join[__dirname, '/client/public']]];
// Views and view engine
app.set['views', path.join[__dirname, '/client/views']];
app.set['view engine', 'ejs'];
app.engine['html', handlebars[{ defaultLayout: 'layout' }]];
Handlebars is not being able to find my layout. Its looking at the app/views/layouts/layout.handlebars
, but it should be looking at app/client/views/layouts/layout.handlebars
What am I missing here ?
asked Dec 20, 2016 at 22:15
1
Seems like you are using express-handlebars
You need to point the directories while creating an instance of it.
const hbs = exphbs.create[{
extname :'hbs',
layoutsDir : 'path/to/layout/directory',
defaultLayout: 'main',
helpers : 'path/to/helpers/directory',
partialsDir : [
'path/to/partials/directory'
]
}];
If you don't initiate it with custom folder locations, it looks up for the files in the default locations.
Also, the view engine needs to be set as hbs.engine
not ejs
app.engine['hbs', hbs.engine];
app.set['view engine', 'hbs'];
steadweb
13.8k3 gold badges28 silver badges40 bronze badges
answered Dec 21, 2016 at 3:40
Swaraj GiriSwaraj Giri
3,9172 gold badges23 silver badges40 bronze badges
1