A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/segmentio/koa-params below:

segment-boneyard/koa-params: Add express style params support to koa

Add express style params support to koa-route.

var koa = require('koa');
var route = require('koa-route')
var paramify = require('koa-params');

route = paramify(route);
var param = route.param;
var get = route.get;

var app = koa();

// fake db
var users = {
  'julian': { name: 'Julian Gruber', fun: true },
  'badguy': { name: 'Bad Guy', fun: false }
};

param('user', function*(id, next){
  var user = users[id];
  if (!user) return this.status = 404;
  this.user = user;
  yield next;
});

app.use(get('/', function*(){
  this.body = Object.keys(users);
}));

app.use(get('/:user', function*(){
  this.body = this.user;
}));

app.listen(3000);

Return a cloned version of route with http verbs patched and added .param.

When a route contains :param, call fn with its value and next. Wait for it to yield before continuing with the real route handler - unless you don't yield and the response ends.

You can register multiple fns per param, just as with express.

fn can be koa middleware too, when fn only takes one argument, next will be supplied.

MIT


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4