-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
118 lines (109 loc) · 3.56 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
"use strict"
var express = require("express");
var passport = require("passport");
var bodyParser = require("body-parser");
var session = require('express-session');
var RedisStore = require('connect-redis')(session);
var redis = require("redis");
var client = redis.createClient();
var flash = require("connect-flash");
var expressValidator = require("express-validator");
var compression = require("compression");
var notification = require('./routes/notification');
var fbLogin = require('./routes/facebookLogin');
var upload = require('./routes/upload');
var profile = require('./routes/profile');
var freeItem = require('./routes/item');
var privacy = require('./routes/privacy');
var prelaunch = require('./routes/prelaunch');
var admin = require('./routes/admin');
var handleErrors = require('./routes/handleErrors');
var facebook = require('./controllers/facebook');
var paypal = require('./controllers/paypal');
var config = require('./config');
var db = require('./models/db');
var moment = require('moment');
var helmet = require('helmet');
// var csp = require('helmet-csp');
moment().format();
var app = express();
// Redirect www
function wwwRedirect(req, res, next) {
if (req.headers.host.slice(0, 4) === 'www.') {
var newHost = req.headers.host.slice(4);
return res.redirect(301, req.protocol + '://' + newHost + req.originalUrl);
}
next();
};
app.set('trust proxy', true);
app.use(wwwRedirect);
app.use(express.static('public'));
app.use('/sweetalert', express.static(__dirname + '/node_modules/sweetalert/dist/'));
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
// app.use(require('cookie-parser')());
app.use(bodyParser.json()); // For parsing forms
app.use(expressValidator());
var sessionMiddleware = session({
store: new RedisStore({ host: 'localhost', port: 6379, client: client, ttl : 60*60*24}),
secret: config.secretKey,
resave: false, // don't save session if unmodified
saveUninitialized: false // don't create session until something stored
});
app.use(sessionMiddleware);
app.use(function (req, res, next) {
var tries = 3;
function lookupSession(error) {
if (error) {
return next(error);
}
tries -= 1;
if (req.session !== undefined) {
return next();
}
if (tries < 0) {
return next(new Error('oh no'));
}
sessionMiddleware(req, res, lookupSession);
}
lookupSession();
})
// // use cookie session instead of express session for lightweight
// app.use(require('cookie-session')({
// // Do we need to use a session store?
// // Cookie maxAge not set yet.
// secret: config.secretKey, // consider using array of keys [SEE: https://github.com/expressjs/cookie-session]
// cookie: {
// maxAge: 365 * 24 * 60 * 60 * 1000
// }
// // Following 2 options not required for cookie session
// // resave: false,
// // saveUninitialized: true
// }));
// Flash messages
app.use(flash());
app.use(function(req, res, next){
res.locals.success_messages = req.flash('success_messages');
res.locals.error_messages = req.flash('error_messages');
next();
});
// Initialize Passport and restore authentication state, if any, from the
// session
app.use(passport.initialize());
app.use(passport.session());
// Define routes
app.use(fbLogin.onlyNotLogout(fbLogin.facebookCache));
app.use(fbLogin.onlyNotLogout(notification.getNotifications))
app.use(helmet());
app.use(helmet.noCache())
fbLogin.route(app);
notification.route(app);
upload(app);
profile(app);
freeItem(app);
paypal(app);
privacy(app);
prelaunch(app);
admin(app);
handleErrors(app);
app.listen(config.portNum);