0% found this document useful (0 votes)
4 views2 pages

Express JS Routing and Middleware Guide

The document provides examples of Express.js applications demonstrating routing, middleware, templating with EJS, and handling form data. It includes code snippets for setting up a server, defining routes, using middleware for logging and custom headers, and managing resources with add, view, and delete functionalities. Additionally, it shows how to render EJS templates to display dynamic content in a web application.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Express JS Routing and Middleware Guide

The document provides examples of Express.js applications demonstrating routing, middleware, templating with EJS, and handling form data. It includes code snippets for setting up a server, defining routes, using middleware for logging and custom headers, and managing resources with add, view, and delete functionalities. Additionally, it shows how to render EJS templates to display dynamic content in a web application.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1(a) Express JS – Routing, HTTP Methods, Middleware

const express = require('express');


const app = express();
const PORT = 2277;

[Link]('/', (req, res) => {


const class1 = [Link] || 'Not specified';
const strength1 = [Link] || 'Not specified';
[Link](`
<h2>Shri Vishnu Engineering College for Women (A)</h2>
<p>Class: ${class1}</p>
<p>Class Strength: ${strength1}</p>
<a href="/students/101">Go to Student 101</a><br>
<a href="/build-url?name=Alice&age=22">Build URL Example</a>
`);
});

[Link]('/students/:id', (req, res) => {


[Link](`<h2>Student Details for ID: ${[Link]}</h2><a href="/">Back</a>`);
});

[Link]('/build-url', (req, res) => {


const url = `/profile/${[Link]}?age=${[Link]}`;
[Link](`<p>Built URL: <a href="${url}">${url}</a></p><a href="/">Back</a>`);
});

[Link]('/profile/:name', (req, res) => {


[Link](`<h2>Profile of ${[Link]}, Age: ${[Link] || 'Not specified'}</h2><a href="
});

[Link](PORT, () => [Link](`Server running on [Link]

1(c) Middleware Demo


const express = require('express');
const app = express();
const port = 3000;

[Link]((req, res, next) => {


[Link](`${[Link]} ${[Link]}`);
next();
});

[Link]((req, res, next) => {


[Link]('X-Custom-Header', 'MiddlewareDemo');
next();
});

[Link]('/', (req, res) => [Link]('Hello from the middleware demo!'));

[Link](port, () => [Link]('[Link]

2(a) Express JS – Templating (EJS)


// app_templating.js
const express = require('express');
const app = express();
[Link]('view engine', 'ejs');

[Link]('/', (req, res) => {


[Link]('index', { title: 'Welcome', message: 'Hello from EJS Template!' });
});

[Link](3000, () => [Link]('[Link]

// views/[Link]
<!DOCTYPE html>
<html>
<head><title><%= title %></title></head>
<body>
<h1><%= message %></h1>
<p>This page is rendered using EJS.</p>
</body>
</html>

2(b) Express JS – Form Data (Add/View/Delete Resource)


const express = require('express');
const app = express();
[Link]('view engine', 'ejs');
[Link]([Link]({ extended: true }));

let resources = {};

[Link]('/', (req, res) => [Link]('index', { resources }));


[Link]('/add', (req, res) => [Link]('add'));
[Link]('/add', (req, res) => {
const { id, name, age } = [Link];
if (!id || !name || !age) return [Link]('All fields required');
resources[id] = { name, age };
[Link]('/');
});
[Link]('/view/:id', (req, res) => {
const resource = resources[[Link]];
if (!resource) return [Link]('Not found');
[Link]('view', { id: [Link], resource });
});
[Link]('/delete/:id', (req, res) => { delete resources[[Link]]; [Link]('/'); });

[Link](3000, ()=>[Link]('[Link]

// views/[Link]
<!DOCTYPE html><html><body>
<h1>All Resources</h1>
<a href="/add">Add New</a>
<ul>
<% for(let id in resources){ %>
<li><%= id %>: <%= resources[id].name %> (Age: <%= resources[id].age %>)</li>
<% } %>
</ul>
</body></html>

You might also like