-
Notifications
You must be signed in to change notification settings - Fork 42
/
server.mean.js
171 lines (167 loc) · 5.57 KB
/
server.mean.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
module.exports = Mean
/**
* Mean Stack Server File
* @name Mean
* @constructor
* @param {Object} opts the options passed to create the server
* @param {Function} done the callback function
* MIT Licensed
*/
var debug = require('debug')('meanstackjs:server')
var forceSSL = require('express-force-ssl')
var fs = require('fs')
var http = require('http')
var https = require('https')
var run = require('./run.js')
var express = require('express')
var auto = require('run-auto')
function Mean (opts, done) {
/**
* self context to be used through out the application
* @this
*/
var self = this
/**
* stores the root directory at dir
* @global
*/
self.dir = __dirname
/**
* stores the options pass in to the server
* @global
*/
self.opts = opts
/**
* stores the run function incase you need to spin up any other servers
* @global
*/
self.run = run
/**
* creates the logging throught the entire app
* @global
*/
self.logger = require('./server/logger.js').logger
/**
* gets & store the current environment
* @global
*/
self.environment = require('./configs/environment.js').get()
/**
* gets & store the current settings based off of the environment
* @global
*/
self.settings = require('./configs/settings.js').get()
/**
* sets the port based on the options or settings
* @global
*/
self.port = self.opts.port || self.settings.https.active ? self.settings.https.port : self.settings.http.port
self.app = express()
// Connect to MongoDb & Register mongoose schemas
/**
* Connect to MongoDb & Register mongoose schemas
* @function
*/
require('./server/db.js').mongoDB(self)
/**
* sets up all middleware & placed after db connect to give middleware access
* @global
*/
self.middleware = require('./server/middleware.js')
/**
* sets up mailer after db connect to give it access
* @global
*/
self.mail = require('./server/mail.js')
/**
* setupRegister > Used to gather all modules to gether and to register them properly
* @function
*/
require('./server/register.js').registerSystemInfo(self)
/**
* setupExpressConfigs > Used to set up expressjs initially, middleware & passport.
* @function
*/
require('./server/config.js').configMiddleware(self)
/**
* setupAuthentication > Used to set up passport authentication & sessions
* @function
*/
require('./server/authentication.js').passport(self)
/**
* setupExpressSecurity > Used to set up helmet, hpp, cors & content length.
* @function
*/
require('./server/security.js').securityMiddleware(self)
/**
* setupExpressHeaders > Used to set up the headers that go out on every route.
* @function
*/
require('./server/headers.js').headersMiddleware(self)
/**
* setupExpressLogger > Used to set up our morgan logger & debug statements on all routes.
* @function
*/
require('./server/logger.js').loggerMiddleware(self)
/**
* setupTools > Used to set up every tool in the tools directory.
* @function
*/
require('./server/tools.js').setupTools(self)
/**
* setupStaticRoutes > Used to set up all system static routes including the main '/*' route with ejs templating.
* @function
*/
require('./server/routes.js').buildRoutes(self)
/**
* errorMiddleware > Used to set up our customer error handler in the server folder. NOTE: This goes after routes because we do not want it potentally default to express error handler
* @function
*/
require('./server/error.js').errorMiddleware(self)
/**
* maxCDN - *** OPTIONAL *** > Used to purge the max cdn cache of the file. We Support MAXCDN
* @function
*/
require('./server/cdn.js').maxCDN(self)
/**
* auto - connectMongoDb : server > Used to finsh the final set up of the server. at the same time we start connecting to mongo and turning on the server.
* @function
*/
auto({
http: function (cb) {
if (!self.settings.http.active && (self.settings.https.active === false) !== (self.settings.http.active === false)) return cb()
http.createServer(self.app).listen(self.settings.http.port, function () {
self.logger.info('HTTP Express server listening on port %d in %s mode', self.settings.http.port, self.environment)
debug('HTTP Express server listening on port %d in %s mode', self.settings.http.port, self.environment)
cb()
})
},
https: function (cb) {
if (!self.settings.https.active) return cb()
https.createServer({
key: fs.readFileSync(self.settings.https.key),
cert: fs.readFileSync(self.settings.https.cert)
}, self.app).listen(self.settings.https.port, function () {
self.logger.info('HTTPS Express server listening on port %d in %s mode', self.settings.https.port, self.environment)
debug('HTTPS Express server listening on port %d in %s mode', self.settings.https.port, self.environment)
cb()
})
},
redirect: function (cb) {
if (self.settings.http.active || !self.settings.https.redirect || !self.settings.https.active) return cb()
var app = require('express')()
app.set('forceSSLOptions', {
httpsPort: self.settings.https.port
})
app.use('/*', forceSSL)
http.createServer(app).listen(self.settings.http.port, function () {
self.logger.info('HTTP FORCE SSL Express server listening on port %d in %s mode', self.settings.http.port, self.environment)
debug('HTTP FORCE SSL Express server listening on port %d in %s mode', self.settings.http.port, self.environment)
cb()
})
}
}, done)
}
if (!module.parent) {
run(Mean)
}