0% found this document useful (0 votes)
10 views8 pages

Express Routing and Modular Structure

Express Routing allows web applications to respond to various HTTP requests and URLs, defined using app.METHOD(PATH, HANDLER). Modular Routing improves code organization and maintainability by separating routes into different files for each feature, such as students and teachers. The document includes examples of basic routing and project structure for implementing modular routing in an Express application.

Uploaded by

gangacbit
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

Express Routing and Modular Structure

Express Routing allows web applications to respond to various HTTP requests and URLs, defined using app.METHOD(PATH, HANDLER). Modular Routing improves code organization and maintainability by separating routes into different files for each feature, such as students and teachers. The document includes examples of basic routing and project structure for implementing modular routing in an Express application.

Uploaded by

gangacbit
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Express Routing and Modular

Routing
What is Express Routing?

• Routing in Express defines how your web app responds to different HTTP requests
and URLs.

Each route is defined as:


[Link](PATH, HANDLER)

Example: [Link]('/', (req, res) => { [Link]('Hello'); });


Example: Basic Routing

• const express = require('express');


const app = express();

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


[Link]('Welcome to Home Page');
});

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


[Link]('About Us Page');
});

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


[Link]('Form Submitted Successfully');
});

[Link](3000, () => [Link]('Server running on port 3000'));


Why Modular Routing?

• In large applications, having all routes in [Link] becomes messy.

✅ Organized code
✅ Easier maintenance
✅ Team collaboration
✅ Scalable and reusable

Each feature (students, teachers, etc.) gets its own route file.
Project Structure

• project/

├── [Link]
└── routes/
├── [Link]
└── [Link]
[Link] (Route Module)

• const express = require('express');


const router = [Link]();

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


[Link]('All Students');
});

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


[Link](`Student ID: ${[Link]}`);
});

[Link] = router;
[Link] (Route Module)

• const express = require('express');


const router = [Link]();

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


[Link]('All Teachers');
});

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


[Link]('New Teacher Added');
});

[Link] = router;
[Link] (Main File)

• const express = require('express');


const app = express();

const studentRoutes = require('./routes/students');


const teacherRoutes = require('./routes/teachers');

// Use route modules


[Link]('/students', studentRoutes);
[Link]('/teachers', teacherRoutes);

[Link](3000, () => [Link]('Server running on port 3000'));

You might also like