This repository has been archived by the owner on Aug 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathexpress.js
261 lines (224 loc) · 6.63 KB
/
express.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
'use strict';
/**
* Module dependencies.
*/
var config = require('../config'),
express = require('express'),
morgan = require('morgan'),
logger = require('./logger'),
bodyParser = require('body-parser'),
session = require('express-session'),
MongoStore = require('connect-mongo')(session),
favicon = require('serve-favicon'),
compress = require('compression'),
methodOverride = require('method-override'),
cookieParser = require('cookie-parser'),
helmet = require('helmet'),
flash = require('connect-flash'),
hbs = require('express-hbs'),
path = require('path'),
_ = require('lodash'),
lusca = require('lusca');
/**
* Initialize local variables
*/
module.exports.initLocalVariables = function (app) {
// Setting application local variables
app.locals.title = config.app.title;
app.locals.description = config.app.description;
if (config.secure && config.secure.ssl === true) {
app.locals.secure = config.secure.ssl;
}
app.locals.keywords = config.app.keywords;
app.locals.googleAnalyticsTrackingID = config.app.googleAnalyticsTrackingID;
app.locals.facebookAppId = config.facebook.clientID;
app.locals.twitterUsername = config.twitter.username;
app.locals.jsFiles = config.files.client.js;
app.locals.cssFiles = config.files.client.css;
app.locals.livereload = config.livereload;
app.locals.logo = config.logo;
app.locals.favicon = config.favicon;
app.locals.env = process.env.NODE_ENV;
app.locals.domain = config.domain;
// Passing the request url to environment locals
app.use(function (req, res, next) {
res.locals.host = req.protocol + '://' + req.hostname;
res.locals.url = req.protocol + '://' + req.headers.host + req.originalUrl;
next();
});
};
/**
* Initialize application middleware
*/
module.exports.initMiddleware = function (app) {
// Should be placed before express.static
app.use(compress({
filter: function (req, res) {
return (/json|text|javascript|css|font|svg/).test(res.getHeader('Content-Type'));
},
level: 9
}));
// Initialize favicon middleware
app.use(favicon(app.locals.favicon));
// Enable logger (morgan) if enabled in the configuration file
if (_.has(config, 'log.format')) {
app.use(morgan(logger.getLogFormat(), logger.getMorganOptions()));
}
// Environment dependent middleware
if (process.env.NODE_ENV === 'development') {
// Disable views cache
app.set('view cache', false);
} else if (process.env.NODE_ENV === 'production') {
app.locals.cache = 'memory';
}
// Request body parsing middleware should be above methodOverride
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(methodOverride());
// Add the cookie parser and flash middleware
app.use(cookieParser());
app.use(flash());
};
/**
* Configure view engine
*/
module.exports.initViewEngine = function (app) {
app.engine('server.view.html', hbs.express4({
extname: '.server.view.html'
}));
app.set('view engine', 'server.view.html');
app.set('views', path.resolve('./'));
};
/**
* Configure Express session
*/
module.exports.initSession = function (app, db) {
// Express MongoDB session storage
app.use(session({
saveUninitialized: true,
resave: true,
secret: config.sessionSecret,
cookie: {
maxAge: config.sessionCookie.maxAge,
httpOnly: config.sessionCookie.httpOnly,
secure: config.sessionCookie.secure && config.secure.ssl
},
name: config.sessionKey,
store: new MongoStore({
db: db,
collection: config.sessionCollection
})
}));
// Add Lusca CSRF Middleware
app.use(lusca(config.csrf));
};
/**
* Invoke modules server configuration
*/
module.exports.initModulesConfiguration = function (app) {
config.files.server.configs.forEach(function (configPath) {
require(path.resolve(configPath))(app);
});
};
/**
* Configure Helmet headers configuration for security
*/
module.exports.initHelmetHeaders = function (app) {
// six months expiration period specified in seconds
var SIX_MONTHS = 15778476;
app.use(helmet.frameguard());
app.use(helmet.xssFilter());
app.use(helmet.noSniff());
app.use(helmet.ieNoOpen());
app.use(helmet.hsts({
maxAge: SIX_MONTHS,
includeSubdomains: true,
force: true
}));
app.disable('x-powered-by');
};
/**
* Configure the modules static routes
*/
module.exports.initModulesClientRoutes = function (app) {
// Setting the app router and static folder
app.use('/', express.static(path.resolve('./public'), { maxAge: 86400000 }));
// Globbing static routing
config.folders.client.forEach(function (staticPath) {
app.use(staticPath, express.static(path.resolve('./' + staticPath)));
});
};
/**
* Configure the modules ACL policies
*/
module.exports.initModulesServerPolicies = function (app) {
// Globbing policy files
config.files.server.policies.forEach(function (policyPath) {
require(path.resolve(policyPath)).invokeRolesPolicies();
});
};
/**
* Configure the modules server routes
*/
module.exports.initModulesServerRoutes = function (app) {
// Globbing routing files
config.files.server.routes.forEach(function (routePath) {
require(path.resolve(routePath))(app);
});
};
/**
* Configure error handling
*/
module.exports.initErrorRoutes = function (app) {
app.use(function (err, req, res, next) {
// If the error object doesn't exists
if (!err) {
return next();
}
// Log it
console.error(err.stack);
// Redirect to error page
res.redirect('/server-error');
});
};
/**
* Configure Socket.io
*/
module.exports.configureSocketIO = function (app, db) {
// Load the Socket.io configuration
var server = require('./socket.io')(app, db);
// Return server object
return server;
};
/**
* Initialize the Express application
*/
module.exports.init = function (db) {
// Initialize express app
var app = express();
// Initialize local variables
this.initLocalVariables(app);
// Initialize Express middleware
this.initMiddleware(app);
// Initialize Express view engine
this.initViewEngine(app);
// Initialize Helmet security headers
this.initHelmetHeaders(app);
// Initialize modules static client routes, before session!
this.initModulesClientRoutes(app);
// Initialize Express session
this.initSession(app, db);
// Initialize Modules configuration
this.initModulesConfiguration(app);
// Initialize modules server authorization policies
this.initModulesServerPolicies(app);
// Initialize modules server routes
this.initModulesServerRoutes(app);
// Initialize error routes
this.initErrorRoutes(app);
// Configure Socket.io
app = this.configureSocketIO(app, db);
return app;
};