
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@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
The npm package @barelyhuman/conrou receives a total of 5 weekly downloads. As such, @barelyhuman/conrou popularity was classified as not popular.
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
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.