Simple route middleware
JavaScript
Latest commit 0d55fcf Feb 26, 2017 @coderhaoxin coderhaoxin update readme
Permalink
Failed to load latest commit information.
test Add .routePath Aug 23, 2016
.gitignore add coverage Sep 16, 2014
.travis.yml test: only test on node 4+ Nov 14, 2015
History.md 3.2.0 Aug 24, 2016
Readme.md update readme Feb 26, 2017
example.js doc: update example Nov 14, 2015
index.js Add .routePath Aug 23, 2016
package.json 3.2.0 Aug 24, 2016

Readme.md

koa-route

Uber simple route middleware for koa.

const _ = require('koa-route');
app.use(_.get('/pets', pets.list));
app.use(_.get('/pets/:name', pets.show));

If you need a full-featured solution check out koa-router, a Koa clone of express-resource.

Installation

$ npm install koa-route

Example

Contrived resource-oriented example:

const _ = require('koa-route');
const Koa = require('koa');
const app = new Koa();

const db = {
  tobi: { name: 'tobi', species: 'ferret' },
  loki: { name: 'loki', species: 'ferret' },
  jane: { name: 'jane', species: 'ferret' }
};

const pets = {
  list: (ctx) => {
    const names = Object.keys(db);
    ctx.body = 'pets: ' + names.join(', ');
  },

  show: (ctx, name) => {
    const pet = db[name];
    if (!pet) return ctx.throw('cannot find that pet', 404);
    ctx.body = pet.name + ' is a ' + pet.species;
  }
};

app.use(_.get('/pets', pets.list));
app.use(_.get('/pets/:name', pets.show));

app.listen(3000);
console.log('listening on port 3000');

License

MIT