-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
130 lines (108 loc) · 3.21 KB
/
app.js
File metadata and controls
130 lines (108 loc) · 3.21 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* green
* app.js base on koa 2
*
* with koa we should add views
*/
process.on('uncaughtException', function(err){
let exception = {
type: 'server',
stack: err.stack || err
};
if(typeof settings !== 'undefined' && settings.monitor){
Monitor.reportException(exception);
}
console.log('app crashed: ----->>> ');
console.log(exception.stack);
setTimeout(function() {
process.exit(1); // should exit after all 'uncaughtException' event calls
}, 3000);
});
let register = require('babel-register');
let polyfill = require("babel-polyfill");
//===== es6 to es5
register({
presets: ["es2015", "stage-0" ],
plugins: ['transform-async-to-generator']
});
let Koa = require('koa');
let logger = require('koa-logger');
let kc = require('koa-controller');
let convert = require('koa-convert');
let koaBody = require('koa-body');
let requestId = require('koa-request-id');
let session = require('koa-session');
let csrf = require('koa-csrf');
console.log('babel transform runtime >>>>>>');
let path = require('path');
let mkdirp = require('mkdirp');
let custom_middlewares = require('./config/http');
let bootstrap = require('./config/bootstrap').bootstrap;
console.log('<<<<<< babel transform end.');
/**
* config app and load settings
*
* app.name: 应用名称
* app.keys: 可以用于 seesion csrf cookie 加密
* app.proxy: 是否允许从代理头部,获取参数
* app.env: 应用环境
*
* app.convert: 用于旧版本的兼容 custom
* 具体参见文档 https://github.com/guo-yu/koa-guide
* */
let app = new Koa(); //启动服务
Object.assign(app, {
name: 'dji-official',
keys: [], //session & cookie keys 启动后设置
basePath: process.cwd(),
proxy: true
});
app.convert = function(mid){ //转换 1.x 的中间件
app.use(convert(mid));
}
app.on('error', function(err, ctx){
Monitor.error('server error: ---------->');
console.log(err.stack);
});
//====== 加载配置,并启动服务
bootstrap(app, function(){
/**
* add middlewares to kao app
* convert 用于旧版本的兼容
* */
app.convert(logger()); //logger
app.convert(requestId());
mkdirp('.tmp/public/uploads'); //创建上传资源存放文件夹
app.use(koaBody({
multipart: true,
formidable: {
keepExtensions: true,
uploadDir: path.join(__dirname, '.tmp/public/uploads'),
//onFileBegin: function (name, file) {}
}
}));
app.convert(session(app));
//app.convert(csrf());
//====== 自定义中间件
custom_middlewares(app);
app.convert(kc.tools()); //optional
app.convert(kc.router({
controllerPath: app.basePath + '/controllers/{controller}Controller.js',
constraintPath: app.basePath + '/constraints/{constraint}Constraint.js',
logger: console.log // custom logger function
}));
//====== start app
let server = app.listen(settings.port || 3000);
// 这一行代码一定要在最后一个app.use后面使用
// 不在线上环境建立 socket 连接
if(settings.publish !== 'production'){
try{
let Socket = require('./services/Socket');
let socket = new Socket(server);
socket.connect(); //建立Socket 连接
}catch(ex){
Monitor.error('sokect connect error:----------->');
console.log(ex);
}
}
});