Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
mini Koa Api
// Dependencies
var koa = require('koa');
var app = koa();
var logger = require('koa-logger');
var route = require('koa-route');
// Db access
var monk = require('monk');
var wrap = require('co-monk');
var db = monk('localhost/koaDemoUsers');
var users = wrap(db.get('users'));
// Middleware
app.use(logger());
// Route
app.use(route.get('/user/:name', getUser));
// Route handling
function *getUser(userName) {
var user = yield users.findOne({name:userName});
if (!user) this.throw(404, 'invalid user name');
this.body = user;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment