app.set and app.engine in Express

怎甘沉沦 提交于 2019-12-02 17:38:57

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!