Laravel-style routing system for Express.js — with full support for CommonJS, ESM, and TypeScript.
- ✅ Laravel-style route grouping and prefixing
- ✅
handle()-based middleware — no need to reference specific methods - ✅ Strict Chaining:
Routes.middleware([Mw]).get(...)or.group(...) - ✅
Routes.apply(app, router)— auto mounts router - ✅
Routes.controller()— auto-register all methods with optional per-method middlewares - ✅
Routes.errorHandler()— global typed error handler - ✅
Routes.maintenance()— toggle maintenance mode - ✅
HttpContextwith{ req, res, next, error }in all handlers - ✅ Static class, instance class, and plain object controllers
- ✅ CommonJS, ESM, and TypeScript support
npm install @refkinscallv/express-routingconst express = require('express')
const Routes = require('@refkinscallv/express-routing')
const app = express()
const router = express.Router()
app.use(express.json())
Routes.get('/', ({ res }) => res.json({ message: 'Hello World' }))
Routes.apply(app, router).then(() => {
app.listen(3000)
})Highly Recommended. When chaining (Routes.middleware([...]).get(...)), middleware MUST be an object or class that implements a handle() method.
class AuthMiddleware {
static handle({ req, res, next }) {
if (!req.headers.authorization) {
return res.status(401).json({ error: 'Unauthorized' })
}
next()
}
}
// Chaining (Strict mode: only handle() allowed)
Routes.middleware([AuthMiddleware]).group('/api', () => { ... })
Routes.middleware([AuthMiddleware]).get('/secured', handler)
// Scoped (with callback)
Routes.middleware([AuthMiddleware], () => {
Routes.get('/secured', handler)
})Only works in scoped calls or route-level injection.
const mw = (req, res, next) => { req.user = 'guest'; next() }
Routes.middleware([mw], () => {
Routes.get('/route', handler)
})Routes.group('/api', () => {
Routes.get('/users', handler) // GET /api/users
Routes.post('/users', handler) // POST /api/users
Routes.group('/v1', () => {
Routes.get('/status', handler) // GET /api/v1/status
})
})class UserController {
static index({ res }) { res.json({ users: [] }) } // GET /users
static myProfile({ res }) { res.json({}) } // GET /users/my-profile
static post_create({ req, res }) { res.json({}) } // POST /users/create
}
// Controller with optional per-method middlewares
Routes.controller('users', UserController, {
'myProfile': AuthMiddleware,
'post_create': [AuthMiddleware, AdminMiddleware]
})class UserController {
static index({ req, res }) { res.json({ users: [] }) }
}
Routes.get('/users', [UserController, 'index'])See API.md for complete documentation.
See CHANGELOG.md.