-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (53 loc) · 1.17 KB
/
index.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
'use strict'
var koa = require('koa'),
bodyParser = require('koa-bodyparser'),
config = require('./libs/config'),
routes = require('./routes/index'),
common = require('koa-common'),
app = koa()
// X-Response-Time
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 - %s - %s ms'
, start.toUTCString()
, this.method
, this.url
, this.status
, ms
)
})
// Body
app.use(bodyParser({
onerror: function(err, ctx) {
ctx.throw('Improper JSON', 422)
}
}))
// Static Middleware
app.use(common.static(__dirname + '/public/dist'))
// Error Handling
app.use(function *(next){
try {
yield next
} catch(e) {
console.error(e.stack || e)
this.status = e.status || 500
this.body = {error: true, msg: e.toString()}
this.app.emit('Error: ', e, this)
}
})
// Endpoints
app.use(routes().routes())
app.listen(config.app.port, config.app.host, function() {
console.log('Listening on http://%s:%s', config.app.host, config.app.port)
})