Discover how to stop repeating yourself and build web pages that update themselves!
Why EJS template setup in Express? - Purpose & Use Cases
Imagine building a website where you have to write the same HTML structure over and over for every page, manually inserting data each time.
Manually copying and pasting HTML with data inserted by hand is slow, repetitive, and easy to make mistakes. Updating the layout means changing every page separately.
EJS templates let you write HTML with placeholders for data. You write the layout once, and EJS fills in the data automatically when the page loads.
res.send("<html><body><h1>Welcome, " + userName + "</h1></body></html>");
res.render('index', { userName: userName });You can create dynamic web pages easily, reusing templates and separating design from data.
A blog site where each post page uses the same template but shows different post content automatically.
Manual HTML duplication is slow and error-prone.
EJS templates separate HTML layout from data.
This makes dynamic page creation simple and maintainable.
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
