- Express.js Basics
- Express.js HOME
- Express.js Introduction
- Express.js Installation
- Express.js Basic App
- Express.js Routing
- Basics Routing
- Route Parameters
- Handling Query Strings
- Router Middleware
- Middleware
- What is Middleware?
- Application-Level Middleware
- Router-Level Middleware
- Built-In Middleware
- Error-Handling Middleware
- Third-Party Middleware
- Express.js HTTP
- Handling GET Requests
- Handling POST Requests
- Handling PUT Requests
- Handling DELETE Requests
- Templating Engines
- Using Templating Engines
- Setting Up EJS
- Setting Up Handlebars
- Setting Up Pug
- Request/Response
- Request Object
- Response Object
- Handling JSON Data
- Handling Form Data
- Static Files
- Serving Static Files
- Setting Up Static Folders
- Managing Assets
- Express.js Advanced
- Middleware Stack
- CORS in Express.js
- JWT Authentication
- Session Handling
- File Uploads
- Error Handling
- Databases
- Express.js with MongoDB
- MongoDB CRUD Operations
- Express.js with MySQL
- MySQL CRUD Operations
- Deployment
- Deploying Express.js Apps to Heroku
- Deploying Express.js Apps to AWS
- Deploying Express.js Apps to Vercel
Express.js Router-Level Middleware
Router-level middleware is similar to application-level middleware but is applied specifically to routes handled by an instance of express.Router(). This allows you to group routes and apply middleware functions only to those routes, enabling more modular and maintainable code.
Key Features of Router-Level Middleware
- Modularization: Middleware functions can be applied to a specific group of routes, improving code organization.
- Scoped to Router: Only the routes within the router instance will use this middleware.
- Can be Combined with Application-Level Middleware: Router-level middleware can be used alongside application-level middleware for more control over how requests are processed.
Steps to Implement Router-Level Middleware
Set up a Basic Express Application with Router
First, initialize a Node.js project and install Express:
npm init -y
npm install express --save
Create a Router and Apply Middleware
Define routes in a separate Router instance and apply middleware specific to those routes:
const express = require('express');
const app = express();
const router = express.Router();
// Define router-level middleware
router.use((req, res, next) => {
console.log('Router-level middleware executed');
next(); // Pass control to the next middleware or route handler
});
// Define a route for the router
router.get('/', (req, res) => {
res.send('Home Route');
});
router.get('/about', (req, res) => {
res.send('About Route');
});
// Attach the router to a specific path
app.use('/router', router);
// Start the server
app.listen(3000, () => {
console.log('Server running on port 3000');
});
Test Router-Level Middleware
Run the application by executing node app.js and visit the routes:
http://localhost:3000/router/http://localhost:3000/router/about
Use Router-Level Middleware for Specific Routes
You can apply different middleware to different routes within the same router:
// Middleware for the '/about' route only
router.use('/about', (req, res, next) => {
console.log('Middleware for /about route');
next();
});
// Define the '/about' route
router.get('/about', (req, res) => {
res.send('About Route with specific middleware');
});
Router-Level Error-Handling Middleware
Error-handling middleware can also be added within the router:
router.use((err, req, res, next) => {
console.error('Error in router:', err.stack);
res.status(500).send('Something went wrong!');
});
Example of Modularized Router with Multiple Middleware
In a real-world application, you might have several routes with their own middleware. Here's an example:
const express = require('express');
const app = express();
const userRouter = express.Router();
// User-specific middleware
userRouter.use((req, res, next) => {
console.log('User router middleware');
next();
});
// User routes
userRouter.get('/profile', (req, res) => {
res.send('User Profile');
});
userRouter.get('/settings', (req, res) => {
res.send('User Settings');
});
// Attach the userRouter to the '/user' path
app.use('/user', userRouter);
const adminRouter = express.Router();
// Admin-specific middleware
adminRouter.use((req, res, next) => {
console.log('Admin router middleware');
next();
});
// Admin routes
adminRouter.get('/dashboard', (req, res) => {
res.send('Admin Dashboard');
});
// Attach the adminRouter to the '/admin' path
app.use('/admin', adminRouter);
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
- The
userRoutermiddleware will run for routes under/user. - The
adminRoutermiddleware will run for routes under/admin.
Summary
Router-level middleware in Express allows for a more modular structure by applying middleware functions to a specific group of routes within an express.Router() instance. This ensures that only relevant routes have the specific middleware applied, enhancing maintainability and code organization. You can use router-level middleware for tasks such as logging, authentication, and error handling, just like application-level middleware.