-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
122 lines (105 loc) · 3.66 KB
/
app.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
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
'use strict'
/*
*
* command line arguments
*
* https : whether to run in HTTPS mode
* ssl_cert : location of ssl certificate file (default: ./certs/key.pem)
* ssl_key : location of ssl key file (default: ./certs/cert.pem)
* ssl_ca : location of ca file if using one (default: ./certs/ca.pem)
*/
//creating a globally viewable basepath (typically bad-practice)
global.__base = __dirname + '/';
let express = require('express'),
path = require('path'),
fs = require('fs'),
app = express(),
http = require('http'),
https = require('https'),
argv = require('yargs').argv, //grabs our app arguments
colors = require('colors'),
Paths = require('./build-config/paths'),
RUNTIME = { PORT : argv.port || 3002 },
codeDir = argv.dev ? Paths.DEST_DEV : Paths.DEST_PROD,
SSL = (()=>
{
if(argv.https)
{
let certPath = argv.ssl_cert,
keyPath = argv.ssl_key,
caPath = argv.ssl_ca,
SSL = { HTTPS : true, options : {} };
if(caPath)
{
SSL.options.ca = fs.readFileSync(caPath);
}
else if(keyPath && certPath)
{
SSL.options.key = fs.readFileSync(keyPath);
SSL.options.cert = fs.readFileSync(certPath);
}
else
{
console.error('https flag requires "cert" and "key" or "ca" file parameters');
process.exit();
}
return SSL;
}
else return { HTTPS : false };
})();
//allow cross-domain data requests
let xDomainMiddleware = (req, res, next)=>
{
res.header('Access-Control-Allow-Origin', "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
};
// disable layout
app.set("view options", {layout: false});
app.set('view engine', 'html');
app.engine('html', require('ejs').renderFile);
//attach middleware to server
app.use(express.static(path.join(__dirname, codeDir)));
app.use(xDomainMiddleware);
console.log(__dirname + '/' + codeDir);
app.get('/', (req,res)=>
{
res.render(__dirname + '/' + codeDir + '/index.html');
});
app.get('*', (req, res)=>
{
res.render(__dirname + '/' + codeDir + '/index.html');
});
let startServer = ((server)=>
{
let host = server.address().address,
port = server.address().port,
version = require('./package.json').version,
appName = require('./package.json').displayName,
protocol= (SSL.HTTPS ? 'https' : 'http');
host = ((host == '0.0.0.0'|| host == '::') ? 'localhost' : host);
console.log('%s '.white + '[v%s] %s', appName, version, argv.dev ? '(DEV MODE)' : '');
console.log('-> currently running at ' + protocol.blue.bold + '://%s:%s'.blue.bold +
' in HTTP %s mode', host, port, SSL.HTTPS ? 'Secure' : 'Insecure');
if(!SSL.HTTPS)
{
console.log('(to enable HTTPS, run app with the flags "https", "ssl_cert" and "ssl_key")');
}
return server;
});
let server = (()=>
{
let server;
if(SSL.HTTPS)
{
server = https.createServer(SSL.options, app).listen(RUNTIME.PORT, ()=>
{
startServer(server);
});
}
else
{
server = http.createServer(app).listen(RUNTIME.PORT, ()=>{ startServer(server); });
}
})();