Utilizing app.set and app.engine functions in the Express framework

I am currently learning from a Node.js tutorial.

There are two lines in the tutorial that I'm not entirely confident about:

app.set('view engine', 'html');
app.engine('html', hbs.__express);

After looking at the documentation for app.set, it simply states:

Assigns setting name to value.

My main question is why it is necessary to use this. Upon searching online, every instance of app.engine seems to have app.set executed beforehand.

If anyone can explain the importance of using app.set before app.engine, please do so.

UPDATE

I came across the following line, but I'm still unclear as this is my first time working with a template engine:

We can instruct Express to treat HTML files as dynamic by utilizing the view engine directive mentioned above.

Answer №1

When working with Express, the app.set function is used to specify the template engine to be used. In this case, the engine being set is html. It is important to have a template engine installed with that name and responsible for files with the .html extension.

If you are using ejs, a single line like the one below is sufficient:

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

However, if you wish to use a template engine for a different file extension, such as wanting the ejs engine to handle both .ejs and .html files, you can use the following line of code. This line specifies that the hbs.__express function should render files with the html extension since there is no specific template engine named html.

The __express function is basically a standard for template engines in Node.js to be compatible with Express. This naming convention allows Express to easily find the rendering function (although it can be configured differently if needed).

I hope this explanation clarifies things for you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to fix the error message ".then is not a function" when using node-7zip

I'm currently utilizing node.js (an Electron app) to handle multiple ZIP archives by using the node-7zip and 7zip-bin libraries. In order to process these files sequentially, as required by the application's logic, I have crafted the subsequent r ...

Concatenating Arrays in nodeJS

Here's a code snippet that deals with arrays: const array = [ "apple", "banana", "cherry" ]; I'm trying to create a multiline string like this: const foo = " apple banana cherry "; Is there a way for me to begin a new line with each it ...

How can you efficiently manage Access & Refresh tokens from various Providers?

Imagine I am allowing my users to connect to various social media platforms like Facebook, Instagram, Pinterest, and Twitter in order to use their APIs. As a result, I obtain access tokens for each of these providers. Based on my research, it seems advisa ...

Tips for transferring client-side data to the server-side in Angular, Node.js, and Express

Seeking a straightforward solution to a seemingly basic question. I am utilizing Angular's $http method with a GET request for a promise from a specific URL (URL_OF_INTEREST). My server runs an express script server.js capable of handling GET reques ...

GlobalThis in Cordova throws an undefined error following the update to version 10.0.0

Hello, I recently developed a Cordova app using version 8.1.0, but encountered an issue when trying to upload it to the Play Store. The error stated that my app targets API LEVEL 28, and I needed to target API level 29 at least. In response, I updated Co ...

The function soap.createClientAsync is not defined and is throwing a TypeError

Encountering an issue with the soap npm module while developing an application in Meteor. Here is the code snippet causing the error: soap.createClientAsync(url).then((client) => { return client.someMethod(soapArgs, function(err, result) { ...

Seeking assistance for my dissertation assignment: electron, express, and SQLite3

I am currently dedicated to my thesis project, which involves designing an educational desktop video game for both public and private schools within my city. Being a fan of electron, I thought it would be a great idea to develop my app using this platform ...

Encountering an issue with launching npm serve on MacOS while using IntelliJ Ultimate for a Vue.js project

I can't seem to get npm serve to start in the Run/Debug Configuration within IntelliJ. However, when I run it separately in the Terminal within IntelliJ or on the regular console, it works just fine. Any idea what's causing this and how to fix ...

obtain the text content from HTML response in Node.js

In my current situation, I am facing a challenge in extracting the values from the given HTML text and storing them in separate variables. I have experimented with Cheerio library, but unfortunately, it did not yield the desired results. The provided HTML ...

Displaying Images Containing Umlauts Might Cause Issues

It has come to my attention that none of the images containing characters like ü,ä,ö are loading on my production server. However, everything works perfectly fine when I serve them locally using nodemon + browsersync. What specific settings do I need t ...

Developing Attributes in JSON

Greetings stackOverflow Community, I'm struggling a bit with creating JSON objects. I have code snippet that is meant to populate a list called members with names, and then add a property to each of those names. Here is the specific snippet in questi ...

Updating a question in Node.js Express: How to specify the WHERE clause?

Today, I find myself struggling to grasp a particular concept. My current task involves using a script to send curls to my program running on server 3000 through node. What I aim to achieve is creating a new entry in my mySQL database each time existing da ...

Preserve object properties while allowing for changes to be made without affecting

I am in need of transferring specific properties from one object to another while ensuring that the original object remains mutable. Here is the original object: var sourceObject = { "key1": "value1", "key2": "value2 ...

Having trouble getting my asynchronous promise to work properly

I am currently working on implementing a login server function and I am struggling to understand why the promise I'm making is not being called. My setup involves using MongoDB with Mongoose as the backend, which is connected to using User.findOne. A ...

Having trouble invoking an established route within a different route in an Express JS project

While working with an Express JS application connected to a mySQL database, I encountered an issue when trying to fetch data from a pre-defined route/query: // customers.model.js CUSTOMERS.getAll = (result) => { let query = "SELECT * FROM custo ...

Do you have any suggestions on how to fix this npm SQLite installation issue?

Could use some help with an SQLite installation error I'm encountering. Any ideas on what the issue might be and how to resolve it? C:\Users\jacka\Downloads\discord-emoji-stealer-master\discord-emoji-stealer-master>npm i & ...

Error: The Redis connection was lost and the command was aborted due to a failed readiness check. It is possible that the command may

Can anyone help me understand the meaning of this error message and what could be causing it? My setup includes node 6.10.0 and redis 2.7.1, with Redis running in a separate Docker container that has been successfully built. I am priming the store with a ...

Has the utilization of stipe medium aircraft resulted in any financial setbacks for me?

After creating a code using Stripe and Node.js, I encountered a dilemma. How can I ensure that the customer has actually paid for their usage of the service? My pricing plan is based on usage, so let's say a user utilized the service 5 times in a mont ...

Middleware in action on all routers following its inclusion

I've encountered an issue when trying to add middleware in express.Router(). let router = express.Router(); let mid = function(req, res, next) { console.log("mid"); next(); } router.get("/", function(req, res) { ...

Executing a NestJs cron job at precise intervals three times each day: a guide

I am developing a notifications trigger method that needs to run three times per day at specific times. Although I have reviewed the documentation, I am struggling to understand the regex code and how to customize it according to my requirements! Current ...