Handlebars is a template engine that is widely used and easy to use. The pages contain .hbs extension and there are many other template engines in the market like EJS, Mustache, etc.
Installation of hbs module:
- You can visit the link Install hbs module. You can install this package by using this command.
npm install hbs
- After installing hbs module, you can check your hbs version in command prompt using the command.
npm version hbs
- After that, you can just create a folder and add a file for example index.js, To run this file you need to run the following command.
- To setup hbs view engine, you need to write this middleware in your index.js as follow:
app.set('views', path.join(__dirname)) app.set('view engine', 'hbs')
- Now create the file and run the code. It will display the result.
Steps to run the program:
- The project structure will look like this:
- Make sure you have installed hbs and express module using the following commands:
npm install hbs npm install express
- Run index.js file using the following command:
- Open browser and type this URL: http://localhost:8080/. Then you will see the Home.hbs page as shown below:
So this is how you can setup Handlebars (hbs) view engine in node.js. There are many other handlebars engines exist like EJS, Mustache, etc.
- How to Setup View Engine in Node.js ?
- HandleBars Templating in ExpressJS
- Nodejs | Automatic restart NodeJs server with nodemon
- Introduction to Model View View Model (MVVM)
- Search Engine Optimization (SEO) | Basics
- Mobile Search Engine Optimization (SEO)
- Elasticsearch Search Engine | An introduction
- Use EJS as Template Engine in Node.js
- How to detect search engine bots with PHP ?
- Difference between Search Engine and Web Browser
- How to use underscore.js as a template engine ?
- Express.js app.engine() Function
- AWS EC2 Instance Setup with Apache Server
- Environment setup for JSP
- Setup API for GeeksforGeeks user data using WebScraping and Flask
- Angular CLI | Angular Project Setup
- p5.js | setup() Function
- ES6 | Environment Setup
- Setup Sending Email in Django Project
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.