server.js
2.71 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
"use strict";
const start_time = Date.now();
const config = require('./init/config'),
MpApiAction = require('./action/mpapi-action'),
WeApiAction = require('./action/weapi-action'),
BzApiAction = require('./action/bzapi-action');
// 定义参数
const BIND_PORT = config.system.bind_port;
// 定义log4js 包含业务日志和系统日志
const logger = require('./init/log4js-init').system;
// 定义db
logger.info('init db');
require('./init/sequelize-init');
// 定义express初始化
logger.info('init express');
const app = new (require('./init/express-init'))();
//加载二级目录使用actions下的模块处理
const routers_path = require('kml-express-stage-lib').routers_path;
let list = routers_path.list();
list.forEach(function (router_path) {
let pattern = `/${router_path}`;
let api_action;
if (/[tc]/.test(router_path)) {
api_action = new MpApiAction(router_path)
} else if(/w/.test(router_path)) {
api_action = new WeApiAction((router_path))
} else {
api_action = new BzApiAction(router_path)
}
app.use(pattern, api_action);
});
//启动服务
const server = app.listen(BIND_PORT, function () {
let os = require('os');
let ifaces = os.networkInterfaces();
let localhost = ['localhost'];
Object.keys(ifaces).forEach(function (ifname) {
let alias = 0;
ifaces[ifname].forEach(function (iface) {
if ('IPv4' !== iface.family || iface.internal !== false) {
// skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
return;
}
if (alias >= 1) {
// this single interface has multiple ipv4 addresses
localhost.push(iface.address);
} else {
// this interface has only one ipv4 adress
localhost.push(iface.address);
}
++alias;
});
});
let server_address = server.address();
let port = server_address.port;
logger.info('Server is listening at: ', localhost.map(function (ip) {
return `http://${ip}:${port}`;
}).join('\n'));
logger.info((ms => `Server startup in ${ms} ms`)(Date.now() - start_time));
});
//bind exception event to log
process.on('uncaughtException', function (e) {
logger.error('uncaughtException from process', e);
if (e.code === 'EADDRINUSE') {
logger.error(`服务端口${BIND_PORT}被占用!`);
process.exit(BIND_PORT);
}
});
process.on('unhandledRejection', (e) => {
logger.warn('unhandledRejection from process', e);
});
process.on('rejectionHandled', (e) => {
logger.warn('rejectionHandled from process', e);
});
// 准备定时器
if (!config.unique_schedule) {
require('./crontab');
}