I am following a Node.js tutorial.
Two lines for which I am not sure are:
app.set('view engine', 'html'); app.engine('html', hbs.__express);
I checked the documentation for app.set
and it only tells me:
Assigns setting name to value.
But my question is what's the relevance of using this. I googled it and wherever app.engine
is used app.set
is called before.
Let me know the significance of using app.set
before the app.engine
.
EDIT
I found the following line, but I am still unclear as I am using template engine very first time:
But we can tell Express to treat HTML files as dynamic by using the
view engine
directive, you see above.
The app. engine() method is used for registering the given template engine callback as "ext". The require() method needs the engine based on the function by default. Use the following methods for engines that do not provide the extensions (or want to map different extensions) or express out of the box.
The app.set() function assigns or sets a setting name to value. This can store any type of value as the user wants, but there are some certain names that can be used to configure the behaviour of the server. Some of the properties that can be configured with the set functionality are − env. etag.
View engines allow us to render web pages using template files. These templates are filled with actual data and served to the client. There are multiple view engines, the most popular of which is Embedded Javascript (EJS).
app. get is called when the HTTP method is set to GET , whereas app. use is called regardless of the HTTP method, and therefore defines a layer which is on top of all the other RESTful types which the express packages gives you access to.
The first line, app.set
tells Express which template engine to use: In this case, html
. This requires that there is a template engine installed with that name, and that this template engine feels responsible for files with the .html
extension.
If you are using ejs
, e.g., this single line is enough (although you usually also have a second call to app.set
that defines the directory where to look for view files):
app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs');
Now, supposed you would like to use a template engine for another file extension, e.g. you would like the ejs
engine not only to take care of .ejs
files, but also of .html
files.
In this case you can use the second line, which tells Express that for files with extension html
you would like to call the hbs.__express
function to render them (as there actually is no template engine called html
). This essentially means that you want the hbs
engine to render .html
files.
The __express
function is a de-facto standard for template engines under Node.js to be Express compatible: That's what their rendering function should be called so that Express can find it easily (if it has a different name, you can configure this as well, but that's a different story).
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With