-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
254 lines (217 loc) · 6.98 KB
/
index.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
var express = require('express');
var path = require('path');
var logger = require('morgan');
var compression = require('compression');
var methodOverride = require('method-override');
var session = require('express-session');
var flash = require('express-flash');
var bodyParser = require('body-parser');
var expressValidator = require('express-validator');
var dotenv = require('dotenv');
var exphbs = require('express-handlebars');
var mongoose = require('mongoose');
var passport = require('passport');
var recaptcha = require('express-recaptcha');
var braintree = require("braintree");
// Load environment variables from .env file
dotenv.load();
var app = express();
///////////////////////////////////////
/////// FAVICON LOCATION ////////
/////////////////////////////////////
var favicon = require('serve-favicon');
try {
app.use(favicon(__dirname + '/public/img/favicon/favicon-16x16.png'));
} catch (err){
console.log('Favicon not found in the required directory.')
}
////////////////////////////////////////////////////
/////// HEROKU VS LOCALHOST .ENV SWAP ////////
//////////////////////////////////////////////////
if (process.env.MONGODB_URI) {
mongoose.connect(process.env.MONGODB_URI);
} else {
mongoose.connect(process.env.MONGODB);
}
mongoose.connection.on('error', function() {
console.log('MongoDB Connection Error. Please make sure that MongoDB is running.');
process.exit(1);
});
//////////////////////////////////////////////////
/////// MONGODB INITIATE CONNECTION ////////
////////////////////////////////////////////////
var db = mongoose.connection;
db.once('open', function() {
// we're connected!
console.log('mongoose connection ok')
//compile the schema for mongoose
});
////////////////////////////////////////////
/////// BRAINTREE INTEGRATION ////////
//////////////////////////////////////////
var gateway = braintree.connect({
environment: braintree.Environment.Sandbox,
merchantId: process.env.MERCHANTID,
publicKey: process.env.PUBLICKEY,
privateKey: process.env.PRIVATEKEY
});
/////////////////////////////////////////////
/////// HTTPS TRAFFIC REDIRECT ////////
///////////////////////////////////////////
// Redirect all HTTP traffic to HTTPS
function ensureSecure(req, res, next){
if(req.headers["x-forwarded-proto"] === "https"){
// OK, continue
return next();
};
res.redirect('https://'+req.hostname+req.url);
};
// Handle environments
if (app.get('env') == 'production') {
app.all('*', ensureSecure);
}
/////////////////////////////////////////////
/////// LOCALHOST PORT SETTING ////////
///////////////////////////////////////////
app.set('port', process.env.PORT || 5000);
//////////////////////////////////////////
/////// GENERAL APP SETTINGS ////////
////////////////////////////////////////
app.use(compression());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(expressValidator());
app.use(methodOverride('_method'));
app.use(session({ secret: process.env.SESSION_SECRET, resave: true, saveUninitialized: true }));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use(function(req, res, next) {
res.locals.user = req.user;
next();
});
app.use(express.static(path.join(__dirname, 'public')));
///////////////////////////////////////////////s
//// SET YOUR APP.JSON DETAILS ////
/////////////////////////////////////////////
var myModule = require('./app.json');
var sitename = myModule.sitename
var website = myModule.website
var repo = myModule.repo
app.locals.sitename = sitename
app.locals.website = website
app.locals.repo = repo
var partialsDir = ['views/partials']
///////////////////////////////
//// ROUTING ////
/////////////////////////////
///////////////////////////////////////////////
//// FRATERNATE NPM MODULE ////
/////////////////////////////////////////////
var fraternate = require("fraternate");
//Append the partial directory inside the NPM module.
partialsDir.push('node_modules/fraternate/views/partials')
app.use('/', fraternate);
/////////////////////////////////////////////////
//// HEAVYLIFTING NPM MODULE ////
///////////////////////////////////////////////
var heavylifting = require("heavylifting");
//Append the partial directory inside the NPM module.
partialsDir.push('node_modules/heavylifting/views/partials')
app.use('/', heavylifting);
/////////////////////////////////////////
/////// HANDLEBARS HELPERS ////////
///////////////////////////////////////
var hbs = exphbs.create({
defaultLayout: 'main',
partialsDir:partialsDir,
helpers: {
ifeq: function(a, b, options) {
if (a === b) {
return options.fn(this);
}
return options.inverse(this);
},
toJSON : function(object) {
return JSON.stringify(object);
},
partial: function (name) {
return name;
},
'dotdotdot' : function(str) {
if (str) {
if (str.length > 16)
return str.substring(0,16) + '...';
return str;}
},
'dotdotdotdot' : function(str) {
if (str) {
if (str.length > 200)
return str.substring(0,200) + '...';
return str;
}
},
'dotdotdotdotdot' : function(str) {
if (str) {
if (str.length > 400)
return str.substring(0,400) + '...';
return str;
}
}, 'dots' : function(str) {
if (str) {
if (str.length > 150)
return str.substring(0,150) + '...';
return str;
}
},
'profile' : function(str) {
if (str) {
if (str.length > 550)
return str.substring(0,550) + '...';
return str;
}
}
}
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
/////////////////////////////
//// 500 ////
///////////////////////////
app.use(function(err, req, res, next) {
// log the error, treat it like a 500 internal server error
// maybe also log the request so you have more debug information
//log.error(err, req);
// during development you may want to print the errors to your console
console.log(err.stack);
req.flash('error', { msg: JSON.stringify(err)});
// send back a 500 with a generic message
res.status(500);
res.redirect('/500');
});
/////////////////////////////
//// 500 ////
///////////////////////////
app.get('/500', function(req, res){
res.render('../../../views/500',{
layout:false
});
});
/////////////////////////////
//// 404 ////
///////////////////////////
app.get('*', function(req, res){
res.render('../../../views/404',{layout:false});
});
// Production error handler
if (app.get('env') === 'production') {
app.use(function(err, req, res, next) {
console.error(err.stack);
res.sendStatus(err.status || 500);
});
}
app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
module.exports = app;