-
Notifications
You must be signed in to change notification settings - Fork 106
/
app.js
64 lines (52 loc) · 1.42 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
const Koa = require('koa')
const koaRouter = require('koa-router')
const json = require('koa-json')
const path = require('path')
const serve = require('koa-static')
const historyApiFallback = require('koa2-history-api-fallback')
const koaBodyparser = require('koa-bodyparser')
const routesObj = require('./server/routes.js')
const app = new Koa()
const router = koaRouter()
app.use(koaBodyparser())
app.use(json())
const fs = require('fs')
function writeLog (data) {
fs.appendFile('./log.txt', data, 'utf8', e => {
})
}
app.use(async (ctx, next) => {
const start = new Date()
const result = await routesObj.verify(ctx)
if (typeof result === 'object') {
ctx.state.userInfo = result
await next()
} else {
writeLog('【' + result + '】')
ctx.body = {
success: false,
data: {},
message: result
}
}
const ms = new Date() - start
writeLog(ctx.method + ' ' + ctx.url + ' ' + ms + 'ms \r\n')
console.log('%s %s - %s', ctx.method, ctx.url, ms)
})
app.on('error', function (err, ctx) {
writeLog('server error' + err + '\n' + JSON.stringify(ctx) + '\r\n')
ctx.body = {
success: false,
data: ctx,
message: err
}
console.log('server error', err)
})
router.use('/api', routesObj.routes.routes())
app.use(router.routes())
app.use(historyApiFallback())
app.use(serve(path.resolve('dist')))
app.listen(3001, () => {
console.log('Koa is listening in 3001')
})
module.exports = app