Selected topic
Templating
Prefer practical output? Use related tools below while reading.
markdown
project/
|---- views/
| |---- index.pug
|---- app.js
|---- package.json
pug
html
head
title My Page
body
h1 Welcome to my page!
p This is a paragraph of text.
javascript
const express = require('express');
const app = express();app.set('view engine', 'pug');
app.get('/', (req, res) => {
res.render('index');
});
app.listen(3000, () => {
console.log('Server listening on port 3000');
});
express as our templating engine using app.set('view engine', 'pug');./) using app.get('/', (req, res) => { ... });.res.render() to render the index.pug template.http://localhost:3000, Pug will compile the template and replace placeholders with actual data.html: defines an HTML blockh1, p: defines a heading or paragraph element, respectively{}: used for string interpolation (e.g., h1= title will render as <h1>title</h1>)=: assigns a value to a variable (e.g., p This is a paragraph of text.)if, for: used for conditional statements and loops, respectively