-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
61 lines (46 loc) · 1.37 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
//jshint esnext
var koa = require('koa');
var staticServer = require('koa-static');
//this allows us to parse the native req object to get the body
var parse = require('co-body');
var router = require('koa-route');
var _ = require('underscore');
var Promise = require('bluebird');
var path = require('path');
var fs = Promise.promisifyAll(require('fs'));
var app = koa();
//our very basic data store
var todos = [];
//gets us unique ids
var counter = (function() {
var count = 0;
return function() {
count++;
return count;
};
})();
//
app.use(staticServer(path.join(__dirname, 'public')));
app.use(router.post('/todos', function *() {
var todo = (yield parse.json(this));
todo.id = counter();
todos.push(todo);
this.body = JSON.stringify(todos);
}));
app.use(router.get('/todos', function *() {
this.body = JSON.stringify(todos);
}));
app.use(router.get('/source', function *() {
var contents = yield fs.readFileAsync('./app.js', 'utf8');
this.body = contents;
}));
app.use(router.delete('/todos/:id', function *(id) {
todos = _(todos).reject(function(todo) {
console.log('what? ', todo, id );
return todo.id === parseInt(id, 10);
}, this);
console.log(todos);
this.body = JSON.stringify(todos.sort(function(a, b) { return a - b;}));
}));
app.listen(3000);
console.log('listening on port 3000');