forked from halloffamezwx/zshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
87 lines (72 loc) · 2.38 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const myLog4js = require("./middleware/my-log4js");
const uuid = require('node-uuid');
const sysLogger = myLog4js.getLogger('system');
sysLogger.addContext('logid', uuid.v4().replace(/-/g,""));
console.log = sysLogger.info.bind(sysLogger);
const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const koaBody = require('koa-body');
const controller = require('./middleware/controller');
const templating = require('./middleware/templating');
const compress = require('koa-compress');
const rest = require('./middleware/rest');
const app = new Koa();
const session = require("koa-session2");
console.log(`process.env.NODE_ENV = [${process.env.NODE_ENV}]`);
const isProduction = process.env.NODE_ENV === 'production';
console.log(`isProduction = [${isProduction}]`);
// log request URL:
app.use(async (ctx, next) => {
var logger = myLog4js.getLogger('zshop');
var logid = uuid.v4().replace(/-/g, "");
logger.addContext('logid', logid);
console.log = logger.info.bind(logger);
ctx.logger = logger;
logger.info(`Process ${ctx.request.method} ${ctx.request.url}...`);
var
start = new Date().getTime(),
execTime;
await next();
console.log("ctx.response.status=" + ctx.response.status);
if (ctx.response.status == 404) {
ctx.response.redirect('/static/html/404.html');
}
ctx.response.set('logid', logid);
execTime = new Date().getTime() - start;
ctx.response.set('X-Response-Time', `${execTime}ms`);
});
app.use(compress({
//filter: function (content_type) {
// return /text/i.test(content_type)
//},
threshold: 2048,
flush: require('zlib').Z_SYNC_FLUSH
}));
app.use(session({
key: "SESSIONID", //default "koa:sess"
maxAge: 30 * 60 * 1000
}));
// static file support:
//if (! isProduction) {
let staticFiles = require('./middleware/static-files');
app.use(staticFiles('/static/', __dirname + '/static'));
//}
app.use(koaBody({
multipart: true,
formidable: {
maxFileSize: 200 * 1024 * 1024 //设置上传文件大小最大限制,默认2M
}
}));
// parse request body:
app.use(bodyParser());
// add nunjucks as view:
app.use(templating('views', {
noCache: !isProduction,
watch: !isProduction
}));
// bind .rest() for ctx:
app.use(rest.restify());
// add controller:
app.use(controller());
app.listen(3000);
console.log('app started at port 3000...');