UNPKG

io-ui-jsonform

Version:

HTML JSON form submission polyfill based on http://darobin.github.io/formic/specs/json/ .

44 lines (33 loc) 974 B
var koa = require('koa'); var app = koa(); var route = require('koa-route'); var send = require('koa-send'); var db = { tobi: { name: 'tobi', species: 'ferret' }, loki: { name: 'loki', species: 'ferret' }, jane: { name: 'jane', species: 'ferret' } }; app.use(route.get('/', index)); app.use(route.get('/pets', list)); app.use(route.post('/pets', addToList)); app.use(route.get('/pets/:name', show)); app.use(function *(){ yield send(this, this.path, { root: './example/client' }); }); app.listen(3005); console.log('listening on port 3005. if proxied on 4000'); function* index(){ yield send(this, '/index.html', {root: './example/client'}); } function* list(){ var names = Object.keys(db); this.body = 'pets: ' + names.join(', '); } function* addToList(){ console.log(this.body + 'hello'); } function* show(name){ var pet = db[name]; if (!pet) return this.throw('cannot find that pet', 404); this.body = pet.name + ' is a ' + pet.species; }