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

koa-power-router

v0.0.7

Published

Yet another router middleware for Koa, but better.

Downloads

13

Readme

koa-power-router

Node version NPM version Dependency Status Travis CI Coveralls

Still under development!

Features:

  • onSTATUS handler (globally or per controller)
  • beforeAction: a function list that runs before router action, if any of them return values except undefined or null will skip action, and go to onSTATUS handler

How to use

Install:

npm install --save koa-power-router

Usage:

var koa = require('koa')
var app = koa()
var router = require('koa-power-router/router')
var Controller = require('koa-power-router/controller')

var options = {
  strictSlash: true // or false
}

app.use(router(options))

router.on('404', function* () {
  yield this.render('404')
})

// Functions and Generators will auto-convert to Controller
router.get('/', function* (next) {
  yield this.render('homepage')
  yield next
})

// Define a Controller with beforeAction
var user = new Controller({
  beforeAction: function () {
    // if not login and token is empty
    if (this.url.indexOf('/login') !== 0 &&
        !this.cookies.get('token')) {
      return true
    }
  },
  login: function* () {
    if (this.method === 'get') {
      yield this.render('login')
    } else {
      doLogin()
    }
  },
  dashboard: function* () {
    yield this.render('dashboard')
  },
  on404: function* () {
    yield this.render('user/404')
  }
})

router.set('/login', ['get', 'post'], user.login)
router.get('/dashboard', user.dashboard)

router.on('404', function* () {
  yield this.render('404')
})

Router

Methods

router.configure(options)

app.use(router())
router.configure({})
// equals to
app.use(router({}))

Supported options:

strictSlash

HTTP RFC 2396 defines path separator to be single slash.

However, unless you're using some kind of URL rewriting (in which case the rewriting rules may be affected by the number of slashes), the uri maps to a path on disk, but in (most?) modern operating systems (Linux/Unix, Windows), multiple path separators in a row do not have any special meaning, so /path/to/foo and /path//to////foo would eventually map to the same file.

http://stackoverflow.com/questions/10161177/url-with-multiple-forward-slashes-does-it-break-anything

If strictSlash is true, URL like //demo will not match the following router:

router.get('/demo', function () {})

router.set(url, methods, action)

url: the pattern of URL, convertd to regular expression with path-to-regexp.

url supports:

  • String: '/homepage'
  • Named string: '/uesr/:id'
  • Regular expression: /^/item/(\d)+/i

methods: the HTTP methods that this router should support, can be string and array.

action: function or generator.

Shortcuts

  • router.all(url, action)
  • router.get(url, action)
  • router.post(url, action)
  • router.put(url, action)
  • router.delete(url, action)

router.on(status, handler)

Register a handler for specified status.

router.on('500', function* (next, error) {
  yield this.render('500')
})

With this feature, you can render error page without redirecting to an actual error page.

Controller

An exmaple of how to define a controller:

var user = new Controller({
  beforeAction: function () {
    if (!this.cookies.get('token')) {
      return true
    }
  },
  dashboard: function* () {
    yield this.render('dashboard')
  },
  on404: function* (){

  }
})

Action

Same as the Koa middleware, nothing different.

dashboard: function* (next) {
  yield this.render('dashboard')
  yield next
}

Methods

on(status, handler)

Register a handler for specified status.

user.on('500', function* () {
  yield this.render('500')
})

If a status is handled by a controller, router's handler will be skipped.

With this feature, you can render error page without redirecting to an actual error page.

Properties

beforeAction

A list of function that run before controller's action. For example:

ar user = new Controller({
  beforeAction: function () {
    if (!this.cookies.get('token')) {
      this.status = 401
      return true
    }
  },
  dashboard: function* () {
    yield this.render('dashboard')
  },
  on401: function* () {
    yield this.render('user/not-authorized')
  }
})

router.set('/login', ['get', 'post'], user.login)
router.get('/dashboard', user.dashboard)

Any request of /dashboard without token will go to on401 handler and render user/not-authorized page instead dashboard page.

Yieldables

Power router uses yieldr to yield generatos and promises, you should notice that yieldr is slightly different from co.

If you really need some features that provided by co, use co directly:

var co = require('co')

router.get('/', function* () {
  var result = yield co(function* () {
    return yield [Promise.resolve('foo')]
  })
  console.log(result) // [ 'foo' ]  
})

Or:

router.get('/', function* () {
  var result = [yield Promise.resolve('foo')]
  console.log(result) // [ 'foo' ]
})

Contributors

Via GitHub