Search code examples
javascriptnode.jsexpressdot.jsexpress-4

why do I get Error: ENOENT open error when trying to render a doT.js view


I am trying out doT.js for the first time and have written a very basic server setup:

'use strict';

var express = require('express');
var app = express();
var doT = require('express-dot');

var pub = __dirname+'/public';
var vws = __dirname+'/views';

app.set('views', vws);
app.set('view engine', 'dot');
app.engine('html', doT.__express);

app.use('/css',express.static(pub + '/css'));
app.use('/img',express.static(pub + '/imgs'));
app.use('/js',express.static(pub + '/js'));

app.get('/', function(req, res){
    res.render(vws + '/index.html', { title: 'dynamic title' });
});

app.listen(8080);
console.log('Server running on port 8080');

When I run the server and goto myaddress:8080 I get the following error: Error: ENOENT, open '/home/myproject/views/layout.html'

If I try calling index.html with res.sendFile it works (although of course I can't pass variables that way)

res.sendFile(vws + '/index.html')

Where am I going wrong? Why am i getting an error which seems to relate to a failed attempt at opening "layout.html" when no example I have seen mentions such a file?

I need to use res.render in order to pass variables to the template, but all the examples I have found do it the same way I tried in my first attempt.


Solution

  • dot.js isn't yet integrated with express 4.0's view engine middleware hook.

    Swig is a similar application that is. It isn't quite as fast as dot.js, but for me it has a great balance of speed and features with an extremely intuitive syntax.

    Full disclosure: I like swig.