Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to lookup view in node.js ejs template while the template is only just renamed

Tags:

node.js

ejs

I have a node.js application rendering in ejs 3 template. let 's say -there is one template, course.ejs, it used to work well in node.js.

res.render('course', locals);

However, today when I tried to change the content, let 's say - course.ejs, it doesn't take effect, there is no error with node.js application and the data passed to the template is all right.

I even copy-pasted the content of this template, and make a new template with a different name - course1.ejs. and change my code to

res.render('course1', locals);

then when the app runs again, it pops up a error saying

Error: Failed to lookup view "course1" template.

The code in node.js and template are all right, it is supposed to work in the ways above. Why it doesn't work now. I have my ejs version 0.8.3, while express in 3.1.0 and node.js in 0.10.0

This is my app configuration.

app.configure(function(){
    app.engine('.html', require('ejs').__express);
    app.set('view engine', 'html');
    app.set('views',__dirname+'/views');

    app.use(express.favicon(__dirname + '/public/favicon.ico'));

  app.use(express.compress({
    filter: function (req, res) {
      return /json|text|javascript|css/.test(res.getHeader('Content-Type'));
    },
    level: 9
  }));  

    app.use(express.bodyParser({uploadDir:__dirname+'/public/uploads',keepExtensions: true,limit: '50mb'}));
    app.use(express.methodOverride());
    app.use(express.cookieParser());
    app.use(express.session({ 
                      cookie: { maxAge: 24 * 60 * 60 * 1000 }
                      ,store: sessionStore
                      ,secret: config.sessionSecret
                      ,key: 'express.sid'
                      ,clear_interval: 3600
    }));


    app.use(express.csrf()); 
    app.use(function(req, res, next){
      res.locals.token = req.session._csrf;
      next();
    });


    app.use(express.static(__dirname+'/public'));   

}

my static files is in the public folder, and all the templates are in the view folder.

I wonder has anyone met this kind of problem before

like image 701
user824624 Avatar asked Apr 19 '13 11:04

user824624


1 Answers

I don't think this configuration can work if your templates have the .ejs extension:

app.engine('.html', require('ejs').__express);
app.set('view engine', 'html');

Using that configuration, Express will look for TEMPLATENAME.html when you call res.render('TEMPLATENAME', ...), and not TEMPLATENAME.ejs. For that, you need this:

app.engine('ejs', require('ejs').renderFile);
app.set('view engine', 'ejs');

So that's the course1 part solved: Express was looking for course1.html, which it couldn't find, so it would generate the error you got (Failed to lookup view...).

However, I don't see how rendering course.ejs could have worked. Perhaps you have another route which is handling the request you're testing? Or you have a course.html in the views folder (besides the course.ejs)?

like image 113
robertklep Avatar answered Oct 22 '22 16:10

robertklep