-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
65 lines (55 loc) · 1.95 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
var express = require('express'),
http = require('http'),
https = require('https'),
fs = require('fs'),
config = require('./config.json'),
routes = require('./src/routes.js'),
httpsApp = express(),
httpApp = express(),
favicon = require('serve-favicon');
//CORS
var allowCrossDomain = function(req, res, next) {
"use strict";
res.header('Access-Control-Allow-Origin', "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'content-type, authorization, content-length, x-requested-with');
next();
};
httpApp.use(favicon(__dirname + '/images/favicon.png'));
httpApp.use(allowCrossDomain); //allow CORS requests
httpApp.use('/images', express.static(__dirname +'/images'));
routes.setup(httpApp);
httpApp.set('port', process.env.PORT || config.http.port);
//CREATE SERVER HTTP
http.createServer(httpApp).listen(httpApp.get('port'), function(){
"use strict";
console.log("http is listening on port", httpApp.get('port'));
});
if(config.https != null) {
//MIDDLEWARE
httpsApp.use(allowCrossDomain); //allow CORS requests
routes.setup(httpsApp);
//SSL MANAGEMENT
var sslOptions = {},
key = config.https.key,
cert = config.https.cert,
ca = config.https.ca;
if(typeof key !== "undefined") {
sslOptions.key = fs.readFileSync(key, 'utf8');
}
if(typeof cert !== "undefined") {
sslOptions.cert = fs.readFileSync(cert, 'utf8');
}
if(typeof ca !== "undefined") {
sslOptions.ca = [];
for(var ci = 0; ci < ca.length; ci++) {
var caFile = ca[ci];
sslOptions.ca.push(fs.readFileSync(caFile, 'utf8'));
}
}
//CREATE SERVER HTTPS
https.createServer(sslOptions, httpsApp).listen(config.https.port, function(){
"use strict";
console.log("http is listening on port", config.https.port);
});
}