npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@barelyhuman/conrou

v0.1.2-alpha.8

Published

> **Con**troller bound **Rou**tes

Downloads

16

Readme

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')
// or
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')
  }
}

// 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

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.

// 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'])

LICENSE

MIT