-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
29 lines (23 loc) · 683 Bytes
/
index.mjs
File metadata and controls
29 lines (23 loc) · 683 Bytes
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
import Koa from 'koa'
import _ from 'koa-route'
const app = new Koa()
const db = {
tobi: { name: 'tobi11', species: 'ferret' },
loki: { name: 'loki', species: 'ferret' },
jane: { name: 'jane', species: 'ferret' }
};
const pets = {
list: (ctx) => {
const names = Object.keys(db);
ctx.body = 'pets: ' + names.join(', ');
},
show: (ctx, name) => {
const pet = db[name];
if (!pet) return ctx.throw('cannot find that pet', 404);
ctx.body = pet.name + ' is a ' + pet.species;
}
};
app.use(_.get('/pets', pets.list));
app.use(_.get('/pets/:name', pets.show));
app.listen(3000);
console.log('listening on port 3000');