Server-Side Templating
Not every app is a JSON API consumed by a separate frontend. Server-side templating generates complete HTML on the server — combining a template (the static markup) with data (from your database) — and sends finished pages to the browser. It's how traditional web apps, admin panels, emails, and server-rendered sites work. This page explains what a template engine is, how Express wires one up, and when server rendering beats a client-side SPA.
What a template engine does
Template (markup + placeholders) Data (from your app)
┌─────────────────────────────┐ ┌──────────────────────┐
│ <h1>Hello, <%= name %>!</h1>│ + │ { name: 'Ada' } │
└─────────────────────────────┘ └──────────────────────┘
│ template engine renders
▼
<h1>Hello, Ada!</h1> ← finished HTML sent to the browserWiring an engine into Express
import express from 'express'
const app = express()
app.set('view engine', 'ejs') // default engine for res.render
app.set('views', './views') // where template files live
app.get('/', (req, res) => {
// Render views/index.ejs with this data:
res.render('index', { title: 'Home', user: req.user })
})The popular engines
Engine | Style | Looks like |
|---|---|---|
Embedded JS in HTML |
| |
Indentation-based, terse | No angle brackets; whitespace-significant | |
Logic-less mustache |
| |
Nunjucks | Jinja-like, powerful |
|
Layouts and partials — don't repeat markup
views/
layout.ejs ← shared shell: <head>, nav, footer
partials/
header.ejs ← reusable fragment
footer.ejs
index.ejs ← page content, included into the layout
users/
list.ejs
show.ejsAuto-escaping — the security default
<%= userInput %> → escapes HTML: <script> (SAFE, default) <%- userInput %> → raw HTML, NOT escaped (DANGEROUS)
Server templating vs client-side SPA
Server templating | Client SPA + JSON API | |
|---|---|---|
HTML built | On the server | In the browser (JS) |
First paint | Fast (HTML arrives ready) | Slower (load JS, then fetch) |
SEO | Excellent (content in HTML) | Needs extra work (SSR/prerender) |
Interactivity | Page reloads / sprinkles of JS | Rich, app-like |
Best for | Content sites, admin, emails | Highly interactive apps |