-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
76 lines (62 loc) · 2.42 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
/**
* 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 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
mongoose.connect(config.mongo.uri, config.mongo.options);
// Setup server
var app = express();
var serverHTTPS = require('https').createServer(credentials, app);
var serverHTTP = require('http').createServer(app);
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'));
});
} 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'));
});
}
// 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;