-
Notifications
You must be signed in to change notification settings - Fork 57
/
app.js
80 lines (74 loc) · 2.75 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
/*global require,process,__dirname,console*/
(function () {
"use strict";
var express = require('express'),
morgan = require('morgan'),
bodyParser = require('body-parser'),
methodOverride = require('method-override'),
cookieParser = require('cookie-parser'),
expressLayout = require('express3-ejs-layout'),
compression = require('compression'),
app = express(),
route = require("./server/route"),
config = require("./server/config"),
kits = require("./server/kits"),
appPackage = require("./package.json"),
localsApp = {
title : config.title,
version: appPackage.version,
env : config.env,
baseUrl: config.baseUrl
};
app.use(compression());
app.use(bodyParser());
app.use(methodOverride());
app.use(cookieParser(config.cookieSecret));
app.set('views', __dirname + '/web/view');
app.use(express.static(__dirname + '/web/static'));// {maxAge: 31557600000}
app.set('view engine', 'html');
app.engine('html', require('ejs').renderFile);
app.use(expressLayout);
app.set('layout', 'layout');
app.set("env", config.env);
app.disable("x-powered-by");
//启用反向代理
app.enable('trust proxy');
morgan.token('data', function (req) {
return "params:" + JSON.stringify(req.params) + ",query:" + JSON.stringify(req.query) + ",body:" + JSON.stringify(req.body);
});
morgan.token('date', function () {
var now = new Date();
return now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate() + " " + now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
});
morgan.token('operationId', function (req) {
return req.context ? req.context.operationId : "null";
});
if ('development' === config.env) {
app.use(morgan({
format: ':method :url :status :remote-addr [:date][:response-time ms] [:operationId]',
stream: {
write: kits.logger.info
}
}));
localsApp.siteScripts = config.siteScripts;
} else {
app.use(morgan({
format: ':method :url :status :remote-addr [:date][:response-time ms] [:operationId]',
stream: {
write: kits.logger.info
}
}));
}
app.locals.app = localsApp;
//初始化路由
route(app);
//create server
app.listen(config.port, function () {
kits.logger.info('ng-nice server listening on port ' + config.port + " in env " + config.env);
});
if (config.env === 'production') {
process.on("uncaughtException", function (err) {
kits.logger.info("process uncaughtException:" + err);
});
}
})();