0

I'm learning Node.js and at template topic right now. First i used only EJS and configured app.set('view engine', 'ejs') for it to work properly. Next to create layout i installed ejs-mate and added app.engine('ejs', require('ejs-mate')) in my code. But what does it actually do? As i understood app.set is configuring my server view engine to be EJS. Thank to this setting our server would know how to parse code of our templates into pure HTML. And res.render would send rendered HTML to client. By app.engine('ejs', require('ejs-mate') am i specifying .ejs files to be rendered by ejs-mate render function? Why do i still need app.set('view engine', 'ejs')? It seems to be working without it.

GTB
  • 57
  • 5

1 Answers1

0

I will try to explain why you need app.set('view engine', 'ejs')

Basic setup using EJS.

let express = require('express');
let app = express();

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

app.get('/', (req, res) => {
  res.render('index', {foo: 'FOO'});
});

app.listen(4000, () => console.log('Example app listening on port 4000!'));

After the view engine is set, you don’t have to specify the engine or load the template engine module in your app. Express loads the module internally. If the view engine property is not set, you must specify the extension of the view file. Otherwise, you can omit it. I hope this explanation gives you better understanding ;-) I know from experience that it is better to stick to the documentation You can read more about it here.

MarioG8
  • 5,122
  • 4
  • 13
  • 29