Reputation: 7628
How can I use dot named file on res.render()
in express.js?
For instance,
There is template file named view.sample.ejs
, I want to render it
app.get('/sample', function(req, res){
res.render('view.sample');
})
The result is,
Error: Cannot find module 'sample'
How can I use dots?
(plus)
I want to name follow mvc model, like
sample.model.js
sample.controller.js
sample.view.ejs
sample.view.update.ejs ...
No problem js file, but render ejs file I couldn't.
Upvotes: 7
Views: 1285
Reputation: 29167
If we look at the library node_modules/express/lib/view.js
, we find that the design of the path to the template everything after the dot in the file name shall be considered as an extension:
this.ext = extname(name); // 'view.sample' => '.sample'
this.name = name; // 'view.sample' => 'view.sample'
And when we try to load the corresponding file extension engine will generate an error:
if (!opts.engines[this.ext]) { // '.sample' engine not found
// try load engine and throw error
opts.engines[this.ext] = require(this.ext.substr(1)).__express;
}
Ok, what to do? Just add the extension:
app.get('/sample', function(req, res){
res.render('view.sample.ejs');
})
Upvotes: 10