3

I would like to configure jade engine to handle .html files in my views folder. Here is my currentserver configuration:

app.configure(function(){
  var pub_dir = __dirname + '/public';
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser());
  app.use(express.session({ secret: nconf.get("site:secret") }));
  app.use(everyauth.middleware());  
  app.use(require('less-middleware')({ src: pub_dir, force:true }));
  app.use(express.static(pub_dir)); 
  app.use(app.router);
  app.use(logErrors);
  app.use(clientErrorHandler);
  app.use(errorHandler);
});
laconbass
  • 17,080
  • 8
  • 46
  • 54
Alex Polkhovsky
  • 3,340
  • 5
  • 29
  • 37

2 Answers2

6

https://github.com/visionmedia/express/blob/master/examples/ejs/index.js

app.engine('.html', require('jade').__express);
3on
  • 6,291
  • 3
  • 26
  • 22
  • 1
    I get this error: Error: Failed to lookup view "mobile/index". If I rename index.html to index.jade, everything works. res.render('mobile/index.html', {title:'Home'}); doesn't work either. – Alex Polkhovsky Aug 20 '12 at 23:48
  • 2
    I figured it out. The problem was line order. This works: app.engine('.html', require('jade').__express); app.set('view engine', 'html'); – Alex Polkhovsky Aug 20 '12 at 23:53
  • 1
    yeah the order can be a freaking nightmare... GladI could help. – 3on Aug 21 '12 at 00:03
  • You probably won't see this but thank you. I banged my head off of this problem for 2 days before I found this post. I never realized that the call order would be playing a factor in it not working. – jamesamuir May 30 '13 at 01:31
  • Just to add in case anyone else comes along, it's important to read the original post's config which is not a "standard config". In particular, note app.use(express.static(..., ...)); I'm saying this because just adding app.engine('.html' ...) and app.set('view engine', 'html') did not suffice in my case like I hoped. – Jon Davis Dec 23 '13 at 03:33
  • For some reasons I couldn't get it working successfully with jade. Had to use ejs module `app.engine('.html', require('ejs').__express);`. See the answer to this related StackOverflow question http://stackoverflow.com/questions/8862936/express-throwing-unexpected-token-indent/18280946#18280946 – dantheta Feb 20 '14 at 10:20
  • app.engine(".html",require('ejs')._express); This statement throws an error "Error: callback function required". What to do? – Rahul Sharma Dec 22 '16 at 10:14
1

Make sure you already have jade in your node_modules

npm install --save jade

In express 4.x, you can simply set the view engine to jade.

app.set('view engine', 'jade')
Wex
  • 15,539
  • 10
  • 64
  • 107