The Express.js router is used to create modular, mountable route handlers. It provides a way for organizing routes and sharing route logic across multiple files.

For example, if you have a website with multiple pages, you can use the Express.js router to create a separate file for each page and mount them all in the main server file.

const express = require(‘express’);
const router = express.Router();

// Home page
router.get(‘/’, (req, res) => {
res.render(‘home’);
});

// About page
router.get(‘/about’, (req, res) => {
res.render(‘about’);
});

// Mount the router in the main server file
app.use(‘/’, router);

Leave a Reply

Your email address will not be published. Required fields are marked *