π Bu README dosyasΔ±nΔ± TΓΌrkΓ§e olarak gΓΆrΓΌntΓΌle
express-smart-router is a powerful and minimalist routing engine for Express.js projects. It automatically loads route files based on folder structure and supports advanced features like versioning, route prefixes, file filtering, and global middleware.
- β
Auto route discovery β Recursively scans
routes/and subdirectories - π
index.js&index.router.jsare bound to the folder root (routes/admin/index.jsβ/admin) - π Extension support β Supports
.js,.router.js,.route.js(configurable) - π
matchfilter β Load only files matching the given RegExp - β¨
baseRouteβ Add a prefix to all routes (e.g./api) - π
middlewareβ Apply global middleware to all routes - π£
verboseβ Log loaded routes to the terminal - π TypeScript support β Ships with
index.d.tstypings - β Cross-platform β Works on Windows, Linux, and macOS
npm install express-smart-routerroutes/
βββ index.js β GET /
βββ hello/
β βββ index.js β GET /hello
βββ auth/
β βββ login.route.js β POST /auth/login
βββ api/
βββ v1/
β βββ index.router.js β /api/v1
β βββ user.router.js β /api/v1/user
βββ v2/
βββ stats.js β /api/v2/stats
express-smart-router supports multiple file extensions for route files:
| Extension | Description |
|---|---|
.js |
General route file. Can be used for single or multiple endpoints. |
.router.js |
Conventionally used for modular route handlers with express.Router(). |
.route.js |
Ideal for single-purpose route files, but also supports modular usage. |
Example:
// user.router.js
const router = require('express').Router();
router.get('/', (req, res) => res.send('User list'));
router.post('/', (req, res) => res.send('Create user'));
module.exports = router;const express = require('express');
const path = require('path');
const smartRouter = require('express-smart-router');
const app = express();
app.use(express.json());
smartRouter(app, path.join(__dirname, 'routes'));
app.listen(3000, () => {
console.log('π Server running at http://localhost:3000');
});smartRouter(app, path.join(__dirname, 'routes'), {
baseRoute: '/api'
});
// routes/user.js β /api/userOnly load .router.js and .route.js files:
smartRouter(app, path.join(__dirname, 'routes'), {
match: /\.(router|route)\.js$/
});Default:
match: /\.js$/const logger = (req, res, next) => {
console.log(`[${req.method}] ${req.url}`);
next();
};
smartRouter(app, path.join(__dirname, 'routes'), {
middleware: [logger]
});smartRouter(app, path.join(__dirname, 'routes'), {
verbose: false
});| File | Route |
|---|---|
routes/index.js |
/ |
routes/index.router.js |
/ |
routes/hello/index.js |
/hello |
routes/api/v1/index.router.js |
/api/v1 |
routes/api/v1/user.route.js |
/api/v1/user |
routes/api/v2/stats.js |
/api/v2/stats |
π Only
index.jsandindex.router.jsare treated as root entry points for folders.
import express from "express";
import smartRouter from "express-smart-router";
import path from "path";
const app = express();
smartRouter(app, path.join(__dirname, "routes"), {
baseRoute: "/api",
match: /\.(router|route)\.js$/,
verbose: true,
});- β Node.js 14+
- β Express 4.x and 5.x
- β TypeScript 4.x+
MIT License
Eren Seyfi
GitHub