Skip to content

Auto-load Express routes from folder structure with support for versioning, file filtering, and global middleware.

License

Notifications You must be signed in to change notification settings

Eren-Seyfi/express-smart-router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“¦ express-smart-router

πŸ” 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.

npm version


πŸš€ Key Features

  • βœ… Auto route discovery – Recursively scans routes/ and subdirectories
  • πŸ“ index.js & index.router.js are bound to the folder root (routes/admin/index.js β†’ /admin)
  • πŸ“„ Extension support – Supports .js, .router.js, .route.js (configurable)
  • πŸ” match filter – 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.ts typings
  • ☁ Cross-platform – Works on Windows, Linux, and macOS

πŸ“¦ Installation

npm install express-smart-router

πŸ“ Example Project Structure

routes/
β”œβ”€β”€ 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

πŸ—‚οΈ Route vs Router Files

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;

πŸ”§ Basic Usage

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');
});

βš™οΈ Configuration Options

πŸ” baseRoute: Global prefix for all routes

smartRouter(app, path.join(__dirname, 'routes'), {
  baseRoute: '/api'
});
// routes/user.js β†’ /api/user

🧠 match: File matching filter using RegExp

Only load .router.js and .route.js files:

smartRouter(app, path.join(__dirname, 'routes'), {
  match: /\.(router|route)\.js$/
});

Default:

match: /\.js$/

πŸ”— middleware: Global middleware applied to all routes

const logger = (req, res, next) => {
  console.log(`[${req.method}] ${req.url}`);
  next();
};

smartRouter(app, path.join(__dirname, 'routes'), {
  middleware: [logger]
});

πŸ“£ verbose: Enable/disable console route logs

smartRouter(app, path.join(__dirname, 'routes'), {
  verbose: false
});

βœ… Route Resolution Rules

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.js and index.router.js are treated as root entry points for folders.


🟦 TypeScript Support

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,
});

πŸ§ͺ Compatibility

  • βœ… Node.js 14+
  • βœ… Express 4.x and 5.x
  • βœ… TypeScript 4.x+

πŸ“ License

MIT License


πŸ‘¨β€πŸ’» Author

Eren Seyfi
GitHub

About

Auto-load Express routes from folder structure with support for versioning, file filtering, and global middleware.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published