-
Notifications
You must be signed in to change notification settings - Fork 1
/
start.js
71 lines (59 loc) · 1.57 KB
/
start.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
/**
* @file 启动
* @author ielgnaw([email protected])
*/
var express = require('express');
var edp = require('edp-core');
var path = require('path');
var fs = require('fs');
/**
* 机器ip
*
* @inner
* @type {string}
*/
var ip = (function () {
var ifaces = require('os').networkInterfaces();
var defultAddress = '127.0.0.1';
var ip = defultAddress;
function x(details) {
if (ip === defultAddress && details.family === 'IPv4') {
ip = details.address;
}
}
for (var dev in ifaces) {
ifaces[dev].forEach(x);
}
return ip;
})();
function start(port) {
var app = express();
app.use(express.static(__dirname + '/public'));
var files = fs.readdirSync(__dirname);
files.forEach(function (file) {
var dir = path.resolve(__dirname, file);
var indexFile = path.resolve(dir, 'index.js');
if (fs.statSync(dir).isDirectory() && fs.existsSync(indexFile)) {
var indexModule = require(indexFile);
if (typeof indexModule.init === 'function') {
indexModule.init(app);
}
}
});
var server = app.listen(port);
edp.log.info('Web start.');
edp.log.info('Visit ' + underlineString('http://localhost:' + port)
+ ' or ' + underlineString('http://' + ip + ':' + port));
edp.log.info('To stop, Press Ctrl+C');
}
start(8002);
/**
* 对输出命令行的字符串添加下划线
*
* @inner
* @param {string} str 源字符串
* @return {string}
*/
function underlineString(str) {
return '\033[4m'+ str + '\033[0m';
}