EJS Template in Node JS with Example


Hi friends, in this tutorial you will learn how to use ejs template in Node JS with an example to render HTML files with the help of a template engine without using any html extension. In order to proceed, let us discuss ejs template.

Also read, How to install Express js in vs code with an example

ejs template is an embedded javascript template engine by which we can display HTML files directly from the folder of your root directory. In this case, the folder name should be “views” by default.

Also read, Write a file into a specific folder in Node JS example

Steps to use ejs template in Node JS

Step 1:- Install the ejs npm package using the below command in the terminal as shown below.

use ejs template in Node JS

Step 2:- Now, you can see the ejs dependency inside the package.json file as shown below.

use ejs template in Node JS

Step 3:- Now, initialize the express JS packages and modules using the const keyword as shown below.

const express = require('express');

Step 4:- Now, declare another variable “data” to use the feature and functionality of express JS as shown below.

const data = express();

Step 5:- Now it is time to create the folder inside the root directory of your project as shown below.

use ejs template in Node JS

Step 6:- Now, create an HTML file inside that folder with the .ejs extension and paste the HTML code below.

about.ejs

<html>
    <title></title>
    <head></head>
    <body>
        <h2>Welcome to about us page</h2>
    </body>
</html>

Step 7:- Now, it is time to initialize the ejs template engine with the help of the set() method as defined below.

data.set('view engine','ejs');

Step 8:- Now, we will use the route along with the request and response parameter to call the HTML file without any html extension as shown below.

data.get('/about',(req,resp)=>{
   resp.render('about');
});

Step 9:- if you want to send some data to the HTML page then you can do so as given below.

data.get('/about',(req,resp)=>{
   //send the data to the html page
   const userdetails = {'name':'Robin','phone':9706417934}
   resp.render('about',{userdetails});
});

Step 10:- Now, update the about.ejs file as shown below.

<html>
    <title></title>
    <head></head>
    <body>
        <h2>Welcome to about us page</h2>
        <h3>Username: <%= userdetails.name %></h3>
        <h4>Username: <%= userdetails.phone %></h4>
    </body>
</html>

Complete Code:- (node js file)

const express = require('express');
const data = express();
data.set('view engine','ejs');
data.get('/about',(req,resp)=>{
   //send the data to the html page
   const userdetails = {'name':'Robin','phone':9706417934}
   resp.render('about',{userdetails});
});
data.listen(3000);

Now, go to the browser and type https://localhost:3000/about and press ENTER then you can see the output as shown below.

Conclusion:- I hope this tutorial will help you to understand the concept. If there is any doubt then please leave a comment below.


Leave a Comment

ceri188

ceri188

ceri188 alternatif

ceri188

link ceri188

ceri188

RATU311

THOR311

THOR311

KUPU178

KUPU178

THOR311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

Togel Online

KUPU178

Situs Togel

KUPU178

ceri188

ceri188

KUPU178

KUPU178

KUPU178

KUPU178

ceri188

ceri188

KUPU178

KUPU178

KUPU178

ratu311

ceri188

ceri188

ratu311

KUPU178

THOR311

ceri188

KUPU178

ceri188

THOR311

THOR311

RATU311

thor311 tajen bali

thor311 alternatif

situs toto4d

thor311 toto4d

thor311 akses

ceri188

KUPU178

daftar ceri188

cery188

ceri188

KUPU178

slot gacor

kupu 178

kupu178

ceri1888

ceri188

slot online

hoki311

togel online

ceri188

ding dong

ratu311

slot gacor

kupu178

live casino

kupu178

mix parlay

ceri188

judi bola online

thor311 slot

HOKI311

THOR311

RATU311

KUPU178

THOR311

THOR311

ceri188

ratu311

slot online

hoki311

kupu178

togel online

kupu178

slot gacor

ceri188

judi bola online

ceri188

ratu311

kupu178

RATU311

RATU311

live casino roullete

ceri188

slot pg soft

kupu178

KUPU178

THOR311

RATU311

KUPU178

KUPU178

THOR311

THOR311

THOR311

thor311 domino qq

thor311 akses

thor dingdong

ceri188

ceri188

KUPU178

kupu178

kupu178

togel

kupu178

mix parlay

ceri188

slot online

kupu178

THOR311

THOR311

KUPU178

KUPU178

kupu178

hoki311

togel online

kupu178

ceri188

ceri188

www.vrcorporate.in

phbalance.vn

togel hongkong

kupu178

slot gacor

kupu178

kupu178

ceri188

kupu178

KUPU178

RATU311

slot online

kupu178

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

toto macau

KUPU178

Slot Gacor

KUPU178

RATU311

Mahjong ways

KUPU178

THOR311

RATU311

KUPU178

KUPU178

RATU311

RATU311

RATU311

Toto Togel

KUPU178

CERI188

Slot Sweet Bonanza 2500

KUPU178

RATU311

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

KUPU178

THOR311

HOKI311

KUPU178

RATU311

Slot Mahjong Ways

KUPU178

CERI188

Slot Online Resmi

KUPU178

RATU311 SLOT ONLINE

KUPU178

KUPU178: Link Akses Resmi

KUPU178: Strategi Menang Di Game Live Casino

KUPU178

KUPU178: Minimal 200 Rupiah

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

CERI188

RATU311: Alternatif karnpuracollege

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

THOR311

RATU311 NONTON BOLA

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

CERI188

RATU311 judi bola

KUPU178

HOKI311

RATU311

THOR311

HOKI311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

KUPU178

RATU311

THOR311