Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

app.set and app.engine in Express

Tags:

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.

like image 277
Trialcoder Avatar asked Apr 09 '14 06:04

Trialcoder


People also ask

What does App Engine do in Express?

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.

What is app set in Express?

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.

What is app set (' view engine EJS ')?

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).

What is the difference between app use and app get in Express js?

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.


1 Answers

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.

like image 151
Golo Roden Avatar answered Jan 05 '23 22:01

Golo Roden