EJS helps you create web pages that change based on data. It mixes HTML with simple code to show dynamic content.
EJS template setup in Express
const express = require('express'); const app = express(); app.set('view engine', 'ejs'); app.get('/', (req, res) => { res.render('index', { title: 'Home Page' }); });
Use app.set('view engine', 'ejs') to tell Express to use EJS templates.
The res.render method loads the EJS file and sends the HTML to the browser.
app.set('view engine', 'ejs');
index.ejs file to the browser and passes a variable user with value 'Alice'.res.render('index', { user: 'Alice' });
views folder by default.// Folder structure: // views/ // index.ejs // partials/ // header.ejs
This Express app uses EJS to render a simple page. The title variable is shown in the page title and heading.
const express = require('express'); const app = express(); app.set('view engine', 'ejs'); app.get('/', (req, res) => { res.render('index', { title: 'Welcome to EJS' }); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); }); // index.ejs file content: // <html> // <head><title><%= title %></title></head> // <body> // <h1><%= title %></h1> // <p>This page is rendered using EJS template.</p> // </body> // </html>
Make sure to install EJS with npm install ejs before running your app.
Keep your EJS files in the views folder unless you change the default path.
You can pass many variables to your EJS templates using the object in res.render.
EJS lets you create dynamic HTML pages easily with Express.
Set EJS as the view engine using app.set('view engine', 'ejs').
Use res.render to send EJS templates with data to the browser.
Practice
app.set('view engine', 'ejs') do in an Express app?Solution
Step 1: Understand the purpose of
This method configures Express settings, here it sets the template engine.app.setStep 2: Identify what 'view engine' setting does
Setting 'view engine' to 'ejs' tells Express to use EJS to render view files automatically.Final Answer:
It tells Express to use EJS as the template engine for rendering views. -> Option CQuick Check:
app.set('view engine', 'ejs') = Use EJS [OK]
- Thinking it installs EJS automatically
- Confusing it with creating files
- Assuming it starts the server
index.ejs with Express?Solution
Step 1: Recall how Express renders templates
Express usesres.renderwith the template name without extension.Step 2: Identify correct syntax
Passing 'index' (without '.ejs') is correct; Express adds the extension automatically.Final Answer:
res.render('index') -> Option DQuick Check:
Use res.render('templateName') without extension [OK]
- Including the .ejs extension in res.render()
- Using res.send() instead of res.render()
- Using non-existent res.view() method
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
res.render('greeting', { name: 'Sam' });
});And the
greeting.ejs file contains:<h1>Hello, <%= name %>!</h1>
Solution
Step 1: Understand data passing to EJS
The object { name: 'Sam' } passes 'Sam' as the value for 'name' in the template.Step 2: Interpret EJS syntax in template
<%= name %>outputs the value of 'name' safely, so it becomes 'Sam'.Final Answer:
<h1>Hello, Sam!</h1> -> Option BQuick Check:
EJS <%= %> inserts variable value [OK]
- Expecting raw template tags to show in output
- Forgetting to pass data object to render
- Confusing <%= %> with <% %> (no output)
const express = require('express');
const app = express();
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
res.render('home.ejs');
});Solution
Step 1: Check usage of res.render
res.render expects the template name without the file extension.Step 2: Identify the error in code
Passing 'home.ejs' includes extension, which causes Express to look for 'home.ejs.ejs'.Final Answer:
The template name in res.render should not include the '.ejs' extension. -> Option AQuick Check:
Use res.render('templateName') without extension [OK]
- Adding .ejs extension in res.render()
- Thinking require('ejs') is mandatory
- Changing app.set order unnecessarily
users.ejs?const users = ['Anna', 'Bob', 'Cara'];
app.get('/users', (req, res) => {
// What goes here?
});Choose the correct code to replace
// What goes here?.Solution
Step 1: Understand how to pass data to EJS
Data must be passed as an object with keys matching template variables.Step 2: Check correct syntax for res.render
Use res.render('templateName', { key: value }) without file extension.Final Answer:
res.render('users', { users: users }); -> Option AQuick Check:
Pass data as object in res.render(template, data) [OK]
- Including .ejs extension in template name
- Passing array directly instead of inside object
- Using res.send instead of res.render
Ready to prove it?
Pass the test and this pattern moves unknown
