-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver-loader.js
74 lines (57 loc) · 2.14 KB
/
server-loader.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
var
// Require necessary npm modules.
path = require('path'),
chalk = require('chalk'),
http = require('http'),
socketIo = require('socket.io'),
nodeStatic = require('node-static'),
// Require local modules.
pkgConfig = require('./package.json'),
ServerConfig = require('./config/server'),
Server = require('./src/js/server'),
// Chalks for logging.
title = chalk.bgBlack.white,
important = chalk.bgGreen.white,
comment = chalk.blue,
error = chalk.red,
processEnv = process.env,
// Pretty date stamp.
date = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, ''),
io,
port,
server,
nodeEnv,
mainServer,
serverConfig,
serverSettings,
staticServer,
staticServerTarget,
staticServerHandler;
console.log(title('====== server-loader start:', date, 'UTC ======'));
// If there is no env, then use development.
if (!processEnv.NODE_ENV) {
processEnv.NODE_ENV = 'development';
}
nodeEnv = processEnv.NODE_ENV;
serverConfig = new ServerConfig(nodeEnv);
serverSettings = serverConfig.getSettings();
mainServer = http.createServer();
io = socketIo(mainServer, { 'transports' : ['websocket'] });
server = new Server();
// Init server side server for business logic.
server.init(io, processEnv, pkgConfig);
// Create a server for static files.
staticServerTarget = path.join(__dirname, 'build', serverSettings.SERVDIR);
staticServer = new nodeStatic.Server(staticServerTarget);
staticServerHandler = function staticServerHandler(req, res) { console.log('=== Request =====>', req.url); staticServer.serve(req, res); };
mainServer.addListener('request', staticServerHandler);
console.log(comment('=== NodeStatic ==>'), important(staticServerTarget));
port = serverSettings.PORT;
mainServer.on('listening', function () {
console.log(comment('=== Server ======>'), important('listening on', port));
});
mainServer.on('error',function (e) {
console.error(error('=== Server ======>'), important(e));
});
mainServer.listen(port, '0.0.0.0');
console.log(comment('=== Server ======>'), important('started on', port));