-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (43 loc) · 1.23 KB
/
app.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const Koa = require('koa');
const app = new Koa();
const route = require('koa-route');
const views = require('koa-views');
const monk = require('monk');
const wrap = require('co-monk');
const parse = require('co-body');
var db = monk('localhost/pokedex');
var pokedex = wrap(db.get('pokemons'));
app.use(views('views', {
map: {
html: 'swig'
}
}));
var pokemons = {
index: function*(){
yield this.render('index');
},
list: function *(){
var pokemons = yield pokedex.find({});
yield this.render('list', {'pokemons': pokemons });
},
show: function *(name){
var pokemon = yield pokedex.findOne({name: name});
yield this.render('show', {'pokemon': pokemon});
},
add: function *(){
if(this.method === 'GET'){
yield this.render('new');
}else{
var post = yield parse(this);
yield pokedex.insert(post);
this.redirect('/pokemons/'+ post.name +'/' );
yield this.render('show', {'pokemon': post});
}
},
};
app.use(route.get('/', pokemons.index));
app.use(route.get('/pokemons', pokemons.list));
app.use(route.get('/pokemons/:name', pokemons.show));
app.use(route.get('/new/', pokemons.add));
app.use(route.post('/new/', pokemons.add));
app.listen(3000);