-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
70 lines (59 loc) · 1.5 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
const Koa = require('koa');
const Router = require('koa-router');
const logger = require('koa-logger');
const bodyParser = require('koa-bodyparser');
const compress = require('koa-compress');
const config = require('config');
const { throwException } = require('./utils/constants/errors');
const seckillModule = require('./src/contrallers/seckill');
// 预加载脚本文件
require('./utils/scripts');
const app = new Koa();
const router = new Router();
console.log(`Version: ${process.version}`);
console.log('node env: ' + process.env.NODE_ENV);
try {
require('./dbs/mysql');
require('./dbs/redis');
} catch (e) {
console.error(e);
process.exit(1);
}
app
.use(logger())
.use(bodyParser())
.use(compress({
filter (content_type) {
return /text/i.test(content_type)
},
threshold: 2048,
gzip: {
flush: require('zlib').constants.Z_SYNC_FLUSH
},
deflate: {
flush: require('zlib').constants.Z_SYNC_FLUSH,
},
br: false // disable brotli
}));
// 添加错误处理方法
app.use(async (ctx, next) => {
ctx.throwException = throwException(ctx);
await next();
});
// 添加正确返回方法
app.use(async (ctx, next) => {
ctx.send = (data) => {
if (data === undefined) data = {};
ctx.body = {
code: 20000,
data,
}
};
await next();
});
router.post('/seckill/:good_id', seckillModule.doSeckill);
app
.use(router.routes())
.use(router.allowedMethods());
app.listen(config.port);
console.log('app start listen: ' + config.port);