-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (81 loc) · 2.93 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const koa = require('koa')
const bodyParser = require('koa-bodyparser')
// const createError = require('http-errors');
const session = require('koa-session');
const cors = require('koa2-cors')
const consola = require('consola')
const PORT = process.env.PORT || 7000;
const path = require('path')
// const koaStatic = require("koa-static");
const routing = require("./routes");
const koaBunyanLogger = require('koa-bunyan-logger');
// register koa
const app = new koa();
// Cross domain
app.use(cors({
exposeHeaders: ['WWW-Authenticate', 'Server-Authorization', 'Date'],
maxAge: 60 * 1000 * 30,
credentials: true,
allowMethods: ['GET', 'PUT', 'POST', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
allowHeaders: ['Content-Type', 'Authorization', 'Accept', 'X-Custom-Header', 'anonymous'],
}));
// setup SessionConfig
const sessionCfg = {
key: 'gamenews:admin-session',
maxAge: 60 * 1000 * 30, //过期时间
autoCommit: true, /** (boolean) automatically commit headers (default true) */
overwrite: true, /** (boolean) can overwrite or not (default true) */
httpOnly: true, /** (boolean) httpOnly or not (default true) */
signed: true, /** (boolean) signed or not (default true) */
rolling: false, /** (boolean) Force a session identifier cookie to be set on every response. The expiration is reset to the original maxAge, resetting the expiration countdown. (default is false) */
renew: false, /** (boolean) renew session when session is nearly expired, so we can always keep user logged in. (default is false)*/
};
app.use(session(sessionCfg, app))
// 配置ctx.body解析中间件
.use(bodyParser())
// Log report
.use(koaBunyanLogger())
.use(koaBunyanLogger.requestIdContext())
.use(koaBunyanLogger.requestLogger({
ignorePath: ['/'],
updateRequestLogFields: function (fileds) {
fileds.module = '请求模块'
},
updateResponseLogFields: function (fileds) {
fileds.module = '回调模块'
}
// updateLogFields: function(fields){
// fields.req_query = this.request.query;
// fields.req_body = this.request.body;
// fields.StatusCode = this.formatResponseMessage.status;
// }
}))
const loggerOptions = {
name: 'games-news'
}
const logger = koaBunyanLogger.bunyan.createLogger(loggerOptions)
// connect mongoDB
// require('./utils/database')(app);
// control error
app.on('error', (err, ctx) => {
if (ctx) {
ctx.log.error({ originErr: err, req: ctx.request }, `错误:${err.message}`);
} else {
logger.error({ originErr: err }, `错误:${err.message}`)
}
})
// 监听全局错误防止漏网之鱼
process.on('uncaughtException', (error) => {
logger.error('uncaughtException', error)
})
process.on('unhandledRejection', (error) => {
logger.error('unhandledRejection', error)
})
// route
routing(app);
// start server
app.listen(PORT, () => {
consola.ready(`服务器已开启: http://localhost:${PORT}/`)
logger.info(`✔ server is opening!`)
})
module.exports = app;