EJS In Node.js Template Example

Zoran Kuzmanovic
2 min readNov 21, 2020

--

What is EJS?
EJS is among the most popular tempate view engines for node.js and express.js.
EJS simply stands for Embedded Javascript. It is a simple templating language that lets users generate HTML with plain javascript.

When to use EJS?
EJS is mostly useful whenever you have “dynamic” content on site and you want to display it. The best example is for online shops. Knowing that your product informations are stored in database. So, from database you are taking that informations and with the help of EJS displaying it on the web page. That proccess is called “dynamic” because you are manipulating with data.

EJS template quick start
This is the base(layout). This way you run EJS as your main view engine:
1. First install express.js and ejs from npm. You do it this way:

npm install express ejs

2. Second create app.js and paste next code:

app.js :

const express= require(‘express’);
const path= require(‘path’);
var app=express();//run EJS
app.set(‘views’,path.join(__dirname,’views’));
app.set(‘view engine’,’ejs’); // run EJS
app.get(‘/’,(req,res)=>{
res.render(‘index’,{myVar:37});
})
app.listen(3000);

The last but not least in the main folder create new folder named “views” and there create index.ejs file:

index.ejs :

<h1> Hello From EJS : <%= myVar %> </h1>

That’s it. Now you are ready to start playing with EJS.

Here is complete EJS tutorial series, short and easy to understand:

https://www.youtube.com/watch?v=IqpfBGsALqc&list=PL7sCSgsRZ-slYARh3YJIqPGZqtGVqZRGt

--

--

Zoran Kuzmanovic
0 Followers

I am web developer with 5 years of experience. This is my website: https://mojuspesansajt.rs/