conrou
Controller bound Routes
Simple abstraction to bind controller classes to routes using action aliases.
Kinda like Laravel/Ruby on Rails
router.post('/users', 'UserController.create')
router.resource('/users', 'UserController')
Installation
npm i @barelyhuman/conrou
Usage
The following uses express.Router
as an example but you can use any router
that has the get
, post
, put
methods with the signature of
(url:string,handler:func)
const _router = express.Router()
const router = createControllerBinder(_router)
class UserController {
me(_, res) {
return res.send('reaper')
}
}
router.register(UserController)
router.register(UserController, {
alias: 'user',
})
router.get('/me', 'UserController.me')
router.get('/me', 'user.me')
router.get('/me', (req, res) => {
console.log('hello')
res.end()
})
router.routeForAction('UserController.me')
router.routeForAction('user.me')
Middleware
Each router interface that you use might have a different way of handling middleware and so
it's not really recommended to use conrou
's middleware utility for this.
Though, conrou does provide a way for you to add in middleware that mimic the execution order similar to
express.
const app = polka()
const router = createControllerBinder(app)
router.register(AuthController)
router.registerMiddleware('auth', (req, res, next) => {
req.currentUser = {
id: 1,
}
next()
})
router
.get('/user', (req, res) => {
console.log(req.currentUser)
return res.end()
})
.middleware(['auth'])
LICENSE
MIT