Node HandleBars Lab 03

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Node Lab 2:

An Organisation wants to render custom HTML templates on their Node.js Server


application. However, they need to integrate Handlebars.js library with their Node.js
application in order to integrate HTML Templates. You are supposed to perform the
following tasks:

• Create a Express Server using Node.js

• Create main layouts file and a home template file

As the developer in the team, you have to perform the following tasks:

Solution:

Task 1: Create Project Directory


1. Enter the following command to create a new project directory

$ mkdir <app-name>
1
2
$ cd <app-name>

Task 2: Initialize package.json file and install


express package
2. In this step, we will initialize the project package.json file using the below command
and install the express package using npm.

$ npm init –y
1
2
$ npm install express –save

Task 3: Set HandleBars Engine as default template


3. In this step, we will set the handlebars.js engine as the default template to render
HTML pages using express routes.
Task 4: Create GET Request that returns HTML
Template
4. In this step, we will add a get request in our server.js file which will return the HTML
template of our express server.

Task 5: Create Views, Layouts Folder and add


main.handlebars file
5. In this step, we will create views folder. Inside the views folder we will create the
layouts folder and add main.handlebars file to it. This is going to be the main layout
file which our application will use to render html markup when the server boots up.

Task 5: Create Home Template


6. In this step, we will create the Home template using handlebars which will be
rendered from the express route.
Task 5: Add Hot Reloading
7. In this step, we will add live re-loading feature to the node application by installing
nodemon package(Add this package as a development dependency)

1
2 $ npm install nodemon –save-dev
Task 8: Start the Server
8. In this step, we can now start the server with the following command.

1
2 $ npm run dev

Task 9: Output
9. Your output should appear similar to the screenshots given below.
Task 8: Conclusion
10. We have completed the following tasks in this Lab.

• We have successfully created Express.js server using node.

• We have successfully integrated Handlebars template engine.


• We have successfully rendered the handlebars template with dynamic data.

• Now time to Discuss the Solution with your Teammates, Trainers and Mentors. Is
there anything where you can improve? Get Feedback on the Output

You might also like