Views in subdirectories with expressjs Views in subdirectories with expressjs node.js node.js

Views in subdirectories with expressjs


I tested it in local and everything works. Just one mistake

from app.set('view engine', 'handlebars'); to app.set('view engine', 'hbs');

my example

var express = require('express'),    app = express();app.engine('html', require('hbs').__express);app.set('views', __dirname+'/views/');app.set('view engine', 'hbs');app.get('/', function(req, res) {    res.render('main/index',{title :"page index"});  });app.listen(3000);


I changed two things and it worked for me.

First

app.set('views',  [path.join(__dirname, 'views'),path.join(__dirname, 'views/main')]);

I passed an array to the app.set and added the subdirectory

Second

res.render('../main/index', {title :"page index"}, function(err, html) {    console.log(err);});

I changed the directory path to relatively navigate from the parent directory.Using the ../ makes it work somehow. Also, once you add the directory to the app.set, you can call index directly e.g., res.render('index',...


Change your set views. it should be
app.set('views', path.join(__dirname, 'views'));