mu-route

0.1.2 • Public • Published

Codeship Status for mu-js/route

mu-route

Simple route middleware for mu, with support for multiple middleware additions to route.

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

Installation

$ npm install mu-route

Example

Contrived resource-oriented example:

var _ = require('mu-route');
var mu = require('mu');
var app = mu();

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

var pets = {
  list: function *(){
    this.body = db;
  },

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

  transform: function *(name) {
    var names = Object.keys(this.body);
    this.body = 'pets: ' + names.join(', ');
  }
};

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

License

MIT

Package Sidebar

Install

npm i mu-route

Weekly Downloads

0

Version

0.1.2

License

MIT

Last publish

Collaborators

  • joshrtay