express-route-mounter
v1.0.2
Published
A utility to map and validate Express routes with duplicate detection
Maintainers
Readme
Express Route Mounter
A powerful and flexible utility for mounting, validating, and managing Express.js routes with comprehensive error handling and duplicate detection.
Features
- Route Validation - Validates route definitions before mounting
- Duplicate Detection - Prevents conflicts by detecting duplicate paths and routes
- Comprehensive Results - Returns detailed information about the mounting process
- Zero Dependencies - Lightweight with no external dependencies (except Express peer dependency)
Installation
npm install express-route-mounterQuick Start
const express = require("express");
const { mountRoutes } = require("express-route-mounter");
// const your route handlers
const authRouter = require("./routes/auth.js");
const userRouter = require("./routes/users.js");
const taskRouter = require("./routes/tasks.js");
// Create an Express app
const app = express();
const apiRouter = express.Router();
// Mount routes with validation
const result = mountRoutes(
[
{ name: "auth", path: "/auth", route: authRouter },
{ name: "users", path: "/users", route: userRouter },
{ name: "tasks", path: "/tasks", route: taskRouter }
],
apiRouter
);
app.use("/api", apiRouter);
app.listen(3000, () => console.log("Server running on port 3000");API Reference
mountRoutes(routes, router, options?)
Mounts an array of route definitions to the provided Express router with validation and duplicate detection.
Parameters
| Parameter | Type | Required | Description |
| --------- | ------------------- | -------- | -------------------------- |
| routes | RouteDefinition[] | Yes | Array of route definitions |
| router | Express.Router | Yes | Express router instance |
| options | MountOptions | No | Optional configuration |
RouteDefinition Interface
An Array of route definitions objects:
interface RouteDefinition {
name: string; // Unique name for the route
path: string; // URL path for the route (e.g., "/users")
route: Express.Router; // Express router instance
}Example:
[
{ name: "auth", path: "/auth", route: authRouter },
{ name: "users", path: "/users", route: userRouter }
// ...
];MountOptions Interface
interface MountOptions {
ignoreDuplicate?: boolean; // Default: false - Whether to ignore duplicate routes
returnResults?: boolean; // Default: true - Whether to return result object
}Return Value
Returns a MountResult object:
interface MountResult {
mounted: Array<{ name: string; path: string }>; // Successfully mounted routes
total: number; // Total number of routes provided
errors: Array<string>; // List of errors encountered
duplicates: {
// Duplicate detection results
paths: Array<DuplicateInfo>;
routes: Array<DuplicateInfo>;
};
}Example:
{
mounted: [
{ path: '/auth', name: 'Route Auth' },
{ path: '/users', name: 'Route User' },
{ path: '/entities', name: 'Route Entity' }
],
total: 3,
errors: [],
duplicates: { paths: [], routes: [] }
}Contributing
Contributions are welcome! Please read our contributing guidelines and submit pull requests to our repository.
License
MIT © Kareem Aboueid
