Experiment 7: ExpressJS – Templating, Form Data
Write a program using templating engine.
Templating Engine in Express.js
A templating engine allows us to generate dynamic HTML pages by combining static HTML with server-side data.
Instead of writing multiple static HTML files, templates enable reusability and dynamic content rendering.
In Express.js, templating engines help separate:
-
Business logic (JavaScript in server)
-
Presentation logic (HTML view)
This follows the MVC (Model–View–Controller) concept, where:
-
Model → data
-
View → template (EJS, Pug, Handlebars)
-
Controller → Express routes
Why use a templating engine?
-
Dynamic content rendering
-
Code reusability (layouts, partials)
-
Cleaner and maintainable code
-
Easy data binding between server and UI
Common templating engines used with Express.js
| Engine | Description |
|---|---|
| EJS | HTML-like syntax, easy to learn |
| Pug | Indentation-based, concise |
| Handlebars | Logic-less templates |
Install Packages
npm init -y
npm install express ejs
Folder structure
templating-demo/
├── app.js
└── views/
└── index.ejs
Express application (app.js)
const express = require("express");
const app = express();
// Set EJS as the templating engine
app.set("view engine", "ejs");
// Route
app.get("/", (req, res) => {
const student = {
name: "Rajesh",
branch: "CSE",
subjects: ["DBMS", "OS", "CN", "DSA"]
};
// Render template and pass data
res.render("index", { student });
});
// Start server
app.listen(3000, () => {
console.log("Server running at http://localhost:3000");
});
EJS Template (
views/index.ejs)<!DOCTYPE html>
<html>
<head>
<title>Express Templating</title>
</head>
<body>
<h1>Student Details</h1>
<p><b>Name:</b> <%= student.name %></p>
<p><b>Branch:</b> <%= student.branch %></p>
<h3>Subjects</h3>
<ul>
<% student.subjects.forEach(subject => { %>
<li><%= subject %></li>
<% }) %>
</ul>
</body>
</html>
Output (Browser)
Student Details
Name: Rajesh
Branch: CSE
Subjects:
- DBMS
- OS
- CN
- DSA
Write a program to work with form data.
HTML Form (
views/form.html)<!DOCTYPE html>
<html>
<head>
<title>Student Form</title>
</head>
<body>
<h2>Student Registration</h2>
<form action="/submit" method="POST">
Name: <input type="text" name="name" required><br><br>
Age: <input type="number" name="age"><br><br>
Branch: <input type="text" name="branch"><br><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
Express Server (
app.js)const express = require("express");
const app = express();
// Middleware to parse form data
app.use(express.urlencoded({ extended: true }));
// Route to display form
app.get("/", (req, res) => {
res.sendFile(__dirname + "/views/form.html");
});
// Route to handle form submission
app.post("/submit", (req, res) => {
const name = req.body.name;
const age = req.body.age;
const branch = req.body.branch;
console.log("name"+name, "age "+age, "branch "+branch);
res.send(`
<h2>Form Data Received</h2>
<p>Name: ${name}</p>
<p>Age: ${age}</p>
<p>Branch: ${branch}</p>
`);
});
// Start server
app.listen(3000, () => {
console.log("Server running at http://localhost:3000");
});
Comments
Post a Comment