-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
94 lines (74 loc) · 2.82 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
/**
* Main application file
*/
'use strict';
// Set default node environment to development
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var express = require('express');
var mongoose = require('mongoose');
var config = require('./server/config/environment');
var https = require('https');
var fs = require('fs');
var path = require('path');
var schedule = require('node-schedule');
var credentials = {
key: process.env.NODE_ENV === 'production' ? fs.readFileSync(path.join(__dirname, '/../../shared/config/ssl.key'), 'utf-8') : fs.readFileSync('./shared/config/ssl.key', 'utf-8'),
cert: process.env.NODE_ENV === 'production' ? fs.readFileSync(path.join(__dirname, '/../../shared/config/ssl.crt'), 'utf-8') : fs.readFileSync('./shared/config/ssl.crt', 'utf-8')
};
// Connect to database
var options = {
server: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } },
replset: { rs_name: 'mongo-rs', socketOptions: { keepAlive: 1, connectTimeoutMS : 30000 }}
}
mongoose.connect(config.mongo.uri, options);
// Setup server
var app = express();
var serverHTTPS = require('https').createServer(credentials, app);
var serverHTTP = require('http').createServer(app);
var rule = new schedule.RecurrenceRule();
rule.minute = 42;
if (process.env.NODE_ENV === 'production') {
serverHTTPS.listen(443, config.ip, function () {
console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
});
serverHTTP.listen(80, config.ip, function () {
console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
});
//schedule indexing of MongoDB
schedule.scheduleJob(rule, function () {
});
} else if (process.env.NODE_ENV === 'development') {
serverHTTP.listen(9000, "localhost", function () {
console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
});
serverHTTPS.listen(4430, "localhost", function () {
console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
});
schedule.scheduleJob(rule, function () {
});
}
// Redirect all requests to https
app.all('*', function (req, res, next) {
if (req.protocol !== 'https') {
res.redirect('https://' + req.get('host') + req.originalUrl);
} else {
next();
}
});
// // For POSTMAN TESTING
// app.all('*', function(req, res, next) {
// console.log(req.url);
// next();
// });
// serverHTTP.listen(9000, config.ip, function () {
// console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
// });
var socketio = require('socket.io')(serverHTTPS, {
serveClient: (config.env === 'production') ? false : true,
path: '/socket.io-client'
});
require('./server/config/socketio')(socketio);
require('./server/config/express')(app);
require('./server/routes')(app);
// Expose app
exports = module.exports = app;