Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
@barelyhuman/conrou
Advanced tools
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')
// or
router.resource('/users', 'UserController')
npm i @barelyhuman/conrou
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')
}
}
// Register the controller to the `router` container
// You are to register an uninstantiated class
router.register(UserController)
// or, you can create an alias
router.register(UserController, {
alias: 'user',
})
router.get('/me', 'UserController.me')
// or, if you created an alias
router.get('/me', 'user.me')
// or
router.get('/me', (req, res) => {
console.log('hello')
res.end()
})
// to get the route for a particular controller, you can use the
// `routeForAction` method on the router.
router.routeForAction('UserController.me') //=> /me
router.routeForAction('user.me') //=> /me
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.
// Using `polka` as an example
const app = polka()
// Register the router as before
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) // 1
return res.end()
})
// tie the named middleware to this particular route
// this syntax / API was inspired by AdonisJS (https://github.com/adonisjs/)
.middleware(['auth'])
FAQs
> **Con**troller bound **Rou**tes
We found that @barelyhuman/conrou demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.