This repository has been archived by the owner on Apr 21, 2021. It is now read-only.
forked from docusign/sample-app-loanco-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
286 lines (240 loc) · 7.22 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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
var express = require('express');
var exphbs = require('express-handlebars');
var fs = require('fs');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var http = require('http');
var _ = require('lodash');
var moment = require('moment');
var passport = require('passport');
var helmet = require('helmet');
// In case of uncaught exception, print the full-stack
process.on('uncaughtException', function(err) {
console.error((err && err.stack) ? err.stack : err);
});
var app = express().use(passport.initialize()).use(helmet());
global.app = app;
app.config = require('./config');
app.helpers = require('./helpers');
app.locals.default_email = app.config.default_email;
// view engine setup
app.engine('hbs', exphbs({
defaultLayout: 'main',
extname: '.hbs',
helpers: {
toJSON : function(object) {
return JSON.stringify(object);
},
ifCond : function(obj1, sign, obj2, options) {
switch(sign){
case '==':
return obj1 == obj2 ? options.fn(this):options.inverse(this);
case '===':
return obj1 === obj2 ? options.fn(this):options.inverse(this);
case '!=':
return obj1 != obj2 ? options.fn(this):options.inverse(this);
case '>=':
return obj1 >= obj2 ? options.fn(this):options.inverse(this);
case '<=':
return obj1 <= obj2 ? options.fn(this):options.inverse(this);
case '>':
return obj1 > obj2 ? options.fn(this):options.inverse(this);
case '<':
return obj1 < obj2 ? options.fn(this):options.inverse(this);
default:
console.error('no sign match:', sign);
break;
}
},
moment: function(context, block){
if (context && context.hash) {
block = _.cloneDeep(context);
context = undefined;
}
var date = moment(context);
var hasFormat = false;
// Reset the language back to default before doing anything else
date.lang('en');
for (var i in block.hash) {
if (i === 'format') {
hasFormat = true;
}
else if (date[i]) {
date = date[i](block.hash[i]);
} else {
console.log('moment.js does not support "' + i + '"');
}
}
if (hasFormat) {
date = date.format(block.hash.format);
}
return date;
}
},
}));
app.set('view engine', 'hbs');
app.set('views', path.join(__dirname, 'views'));
// // uncomment after placing your favicon in /public
// app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.models = require('./models');
var session = require('express-session');
app.use(session({
secret: 'kjh2398sh2nlsd',
resave: false,
saveUninitialized: false
}));
// set up a route to redirect http to https (in case dns not setup)
app.get('*',function(req,res,next){
console.log('Incoming request...');
console.log('hostname:', req.hostname);
console.log('origin:', req.origin);
console.log('url:', req.url);
console.log('headers:', JSON.stringify(req.headers));
console.log('body:', req.body);
if(app.config.force_https && !req.secure){
console.log('Redirecting to https');
var domain = req.hostname;
return res.redirect('https://' + domain + req.url);
}
next();
});
app.use('/', function(req, res, next){
console.log('req.session start:', JSON.stringify(req.session));
// setup session id if not already set
if(!req.session.id){
req.session.id = require('guid').raw();
}
// update session settings
req.session.config = req.session.config || {};
var defaultsToUse = [
'signing_location',
'authentication',
'access_code'
];
_.each(defaultsToUse, function(key){
req.session.config[key] = (key in req.session.config) ? req.session.config[key] : app.config[key];
});
console.log('req.session end:', JSON.stringify(req.session));
next();
});
app.use('/', require('./routes/index'));
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
console.error(err.stack);
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err.stack
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
// Create the HTTP and HTTPS servers
var server = require('http').Server(app);
var httpsServer;
try {
var privateKey = fs.readFileSync('sslcerts/server.key');
var certificate = fs.readFileSync('sslcerts/server.crt');
var credentials = configWithCryptoOptions({
key: privateKey,
cert: certificate
});
httpsServer = require('https').Server(credentials, app);
}catch(err){
console.error('HTTPS server failed to start. Missing key or crt');
}
app.setup = require('./setup');
////////////////////////////////////////////////
// Start the server
////////////////////////////////////////////////
server.listen(3801, function() {
console.log('HTTP being served on 3801');});
httpsServer && httpsServer.listen(4443, function() {
console.log('HTTPS being served on 4443');});
server.on('error', onError);
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
function configWithCryptoOptions(serverConfig) {
serverConfig.ciphers = [
'ECDHE-RSA-AES128-GCM-SHA256',
'ECDHE-ECDSA-AES128-GCM-SHA256',
'ECDHE-RSA-AES256-GCM-SHA384',
'ECDHE-ECDSA-AES256-GCM-SHA384',
'DHE-RSA-AES128-GCM-SHA256',
'ECDHE-RSA-AES256-SHA384',
'DHE-RSA-AES256-SHA384',
'ECDHE-RSA-AES256-SHA256',
'DHE-RSA-AES256-SHA256',
'ECDHE-RSA-AES128-SHA256',
'DHE-RSA-AES128-SHA256',
'HIGH',
'!aNULL',
'!eNULL',
'!EXPORT',
'!DES',
'!RC4',
'!MD5',
'!PSK',
'!SRP',
'!CAMELLIA',
'!ECDH',
'!DSS',
'!RSA-3DES-EDE-CBC-SHA',
'!RSA-AES256-GCM-SHA384',
'!RSA-AES256-CBC-SHA256',
'!RSA-AES256-CBC-SHA',
'!RSA-AES128-GCM-SHA256',
'!RSA-AES128-CBC-SHA256',
'!RSA-AES128-CBC-SHA',
'!ECDHE-RSA-3DES-EDE-CBC-SHA',
].join(':');
serverConfig.honorCipherOrder = true;
const constants = require('constants');
serverConfig.secureOptions = constants.SSL_OP_NO_TLSv1_1 | constants.SSL_OP_NO_TLSv1 | constants.SSL_OP_NO_SSLv3 | constants.SSL_OP_NO_SSLv2;
return serverConfig;
}
module.exports = app;