-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
68 lines (56 loc) · 1.68 KB
/
index.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
require('colors');
var $ = require('./lib/utils');
var http = require('http');
var path = require('path');
var ipv4 = require('ipv4');
global.express = require('express');
var cons = require('consolidate');
module.exports = function(port) {
global.app = express();
var server = require('http').Server(app);
global.io = require('socket.io')(server, {
log: false,
origins: '*:*' //解决同源策略
});
var execDir = process.cwd();
var rootDir = path.join(__dirname);
var assetDir = path.join(rootDir, 'assets');
var projectConfig = {
folders: [execDir],
curDir: execDir
};
var config = {
rootDir: rootDir,
assetDir: assetDir,
reloadJS: path.join(assetDir, 'reload.js'),
settingsPath: '/_',
port: port || 8800,
execDir: execDir,
hosts: ['127.0.0.1', ipv4],
tmplDir: path.join(assetDir, 'settings/dist'),
projectConfig: projectConfig
};
app.engine('html', cons.ejs);
app.engine('jade', cons.jade);
app.engine('hbs', cons.handlebars);
app.set('view engine', 'html');
//添加日志
app.use(require('morgan')('dev'));
//添加settings
app.all('/:pattern(*)',
require('./lib/server')(config)
);
server.listen(config.port, function() {
var port = config.port;
var url = 'http://' + ipv4 + (port ? (':' + port) : port) + '/_/';
$.log(('Mock Server is listening on port ' + config.port).green);
$.log('setting Url is ==> '.red + url.red.bold);
});
process.on('uncaughtException', function(err) {
if (err.errno === 'EADDRINUSE' || err.errno === 'EACCES') {
$.log(('Mock Server: Port ' + config.port + ' is already in use.').red);
process.exit(1);
} else
$.log(err);
});
}