-
Notifications
You must be signed in to change notification settings - Fork 45
/
example.js
31 lines (24 loc) · 655 Bytes
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
'use strict';
var r = require('./');
var Koa = require('koa');
var app = new Koa();
var db = {
tobi: { name: 'tobi', species: 'ferret' },
loki: { name: 'loki', species: 'ferret' },
jane: { name: 'jane', species: 'ferret' }
};
var pets = {
list: (ctx) => {
var names = Object.keys(db);
ctx.body = 'pets: ' + names.join(', ');
},
show: (ctx, name) => {
var pet = db[name];
if (!pet) return ctx.throw('cannot find that pet', 404);
ctx.body = pet.name + ' is a ' + pet.species;
}
};
app.use(r.get('/pets', pets.list));
app.use(r.get('/pets/:name', pets.show));
app.listen(3000);
console.log('listening on port 3000');