-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
103 lines (81 loc) · 2.88 KB
/
server.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
'use strict';
/**
* Module dependencies.
*/
var init = require('./config/init')(),
config = require('./config/config'),
mongoose = require('mongoose'),
https = require('https'),
http = require('http'),
fs = require('fs'),
secrets = require('./config/secrets'),
path = require('path'),
constants = require('constants'),
tls = require('tls'),
compression = require('compression');
/**
* Main application entry file.
* Please note that the order of loading is important.
*/
// Bootstrap db connection
var db = mongoose.connect(config.db);
// Init the express application
var app = require('./config/express')(db);
function shouldCompress(req, res) {
if (req.headers['x-no-compression']) {
// don't compress responses with this request header
return false;
}
// fallback to standard filter function
return compression.filter(req, res);
}
app.use(compression({filter: shouldCompress}));
// print all routers
/*for (var key in app._router.stack) {
var obj = app._router.stack[key];
if(obj.route !== undefined){
console.log(obj.route.path);
}
}
*/
// Bootstrap passport config
require('./config/passport')();
// Start the app on http by listening on <port>
if (config.usehttp) {
var server = http.createServer(app);
server.on('listening',function(){
console.log('ok, http server is running on port ' + config.port);
});
server.listen(config.port);
}
// set certicicates and start SSL server
if (config.usessl) {
var sslconfig = {};
if(config.hasOwnProperty('pfx_file')){
sslconfig.pfx = fs.readFileSync(path.resolve(__dirname, config.pfx_file), 'UTF-8');
}
else if (config.hasOwnProperty('key_file') && config.hasOwnProperty('cert_file')){
sslconfig.key = fs.readFileSync(path.resolve(__dirname, config.key_file), 'UTF-8');
sslconfig.cert = fs.readFileSync(path.resolve(__dirname, config.cert_file), 'UTF-8');
}
if(config.hasOwnProperty('ca_file') && config.hasOwnProperty('ca2_file')){
sslconfig.ca = [
fs.readFileSync(path.resolve(__dirname, config.ca_file), 'UTF-8'),
fs.readFileSync(path.resolve(__dirname, config.ca2_file), 'UTF-8')
];
} else if(config.hasOwnProperty('ca_file')){
sslconfig.ca = fs.readFileSync(path.resolve(__dirname, config.ca_file), 'UTF-8');
}
if(secrets.certificate.passphrase) {
sslconfig.passphrase = secrets.certificate.passphrase;
}
sslconfig.secureProtocol = 'SSLv23_method';
sslconfig.secureOptions = constants.SSL_OP_NO_SSLv3;
var sslServer = https.createServer(sslconfig, app);
sslServer.on('listening',function(){
console.log('ok, https server is running on port ' + config.sslport);
});
sslServer.listen(config.sslport);
}
// Expose app
exports = module.exports = app;