-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
72 lines (56 loc) · 1.27 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
71
72
/**
* Created by trigged on 7/25/15.
*/
"use strict"
require('node-extensions')
require('babel/register')
const koa = require('koa')
, app = koa()
, body = require('koa-body')()
, router = require('koa-router')()
, music = require("./api/music")
, hook = require("./api/hook")
app.use(function *(next) {
var start = new Date();
yield next;
var ms = new Date() - start;
this.set('X-Response-Time', ms + 'ms');
});
// logger
app.use(function *(next) {
var start = new Date();
yield next;
var ms = new Date() - start;
console.log('%s %s - %s', this.method, this.url, ms);
});
app.use(function* (next) {
try {
yield next
} catch (e) {
this.body = {
code: 1,
error_desc: e instanceof Error ? e.message : e,
data: {}
}
console.error(e.stack)
return
}
if (this.data) {
this.body = {
code: 0,
data: this.data || {}
}
}
yield next
})
app.use(router.routes());
app.use(router.allowedMethods());
router.get('/songs', music.list);
router.post('/songs', body, music.add);
router.delete('/songs', music.remove);
router.post('/hook_receive', body, hook.receive);
router.all('/', function* () {
this.body = 'Powered by PUBU.IM(c)'
})
app.listen(process.env.PORT || 3000);
require('./libs/mdns')(app)