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

v0.2.0

Published

Another Koa routing library

Downloads

10

Readme

koa-rutt

Build Status codecov.io

Swedish routing, for Koa.

Install

npm install koa-rutt

Usage

Most features will be demonstrated below.

var app = require('koa')();
    Router = require('koa-rutt');

var router = new Router();

// Add pre-middleware.
router
.pre(function*(next) {
   // This will run on all HTTP methods first (GET, POST, etc);
})
.pre('post', function*(next) {
  // Run a middleware just on this HTTP method (POST), such as a body parser
})
.get('/', function*(next) {
  this.body = "Index!";
})
.get('/item/:id', function(next) {
  // a `params` object is added to `this`.
  var id = this.params.id;
})
.post('/item/:id', function(next) {
  // You can use this just like koa-router, although repeating yourself
  // does get annoying.
})
.route('/item2/:id', {
  // How about we define them all at once?
  get: function*(next) {
    var id = this.params.id;
  },
  post: function*(next) {
    // Oh this is much better!
  }
});

// You can also return individual Route objects if you prefer
router
.route('/some/route')
.get(function*(next) {
  // ...
})
.post(function(next) {
  // ...
})
.delete(function(next) {
  // ...
}),
.put(function(next) {
  // ...
});

// Add the router as middleware to the app.
app.use(router.middleware());

app.listen(3000);

API


class Route


new Route (router, path)

The route.

| Name | Type | Attributes | Description | | ---- | ---- | ---------- | ----------- | | router | Router | | The Router this Route is attached to. | | path | String | | The URL path. |


Route#get|post|delete|put (...middleware) → {Route}

Assign middleware to be run upon relevant HTTP method being triggered.

| Name | Type | Attributes | Description | | ---- | ---- | ---------- | ----------- | | middleware | GeneratorFunction | multiple | Middleware to be attached to called HTTP method. |

Returns: Route Returns this instance of Route.


class Router


new Router ([options])

The router.

| Name | Type | Attributes | Description | | ---- | ---- | ---------- | ----------- | | [options] | Object | optional | Options object | | options.prefix | string | | The prefix of each route of this router. |


Router#get|post|delete|put (path, ...middleware) → {Router}

Assign middleware to be run upon relevant HTTP method being triggered.

| Name | Type | Attributes | Description | | ---- | ---- | ---------- | ----------- | | path | String | | The path to the relevant Route. | | middleware | GeneratorFunction | multiple | Middleware to be attached to called HTTP method. |

Returns: Router Returns this instance of Router.


Router#route (path, [methods]) → {Router|Route}

Create or get a Route from the Router object, or HTTP methods on Route by using the optional methods parameter.

| Name | Type | Attributes | Description | | ---- | ---- | ---------- | ----------- | | path | String | | The URL path to the resource. | | [methods] | Object | optional | An object with HTTP methods. |

Returns: Router|Route Returns this instance of Router, or Route for path if no methods specified.

Example: Usage with optional methods parameter

router.route('/test', {
  * get(next) {
    // Do something
  },
  * post(next) {
    // Do something
  }
})

Example: Usage with only path parameter

const testRoute = router.route('/test')

Router#pre ([method], ...middleware) → {Router}

Define middleware to run prior to HTTP method middleware. If no method provided, the middleware will run before all other middlewares on the router.

| Name | Type | Attributes | Description | | ---- | ---- | ---------- | ----------- | | [method] | string | optional | The HTTP method (eg 'get') to add pre-middleware to. | | middleware | GeneratorFunction | multiple | The middleware to attach. |

Returns: Router Returns this instance of Router.

Example: Example of #pre usage.

router.pre(function* (next) {
  this.type = 'application/json'
  yield next
}).pre('post', bodyParser())

Router#middleware () → {GeneratorFunction}

Returns the middleware to be provided to the Koa app instance.

Returns: GeneratorFunction Middleware to provide to Koa.

License

BSD 2-clause license. See LICENSE.