-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
249 lines (197 loc) · 7.32 KB
/
app.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
//Dependencies
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const morgan = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const expressHbs = require('express-handlebars');
const mongoose = require('mongoose');
const passport = require('passport');
const session = require('express-session');
const flash = require('connect-flash');
const expressValidator = require('express-validator')
const compression = require('compression')
const helmet = require('helmet')
const mongodb = require('mongodb')
const csrf = require('csurf')
const redis = require("redis");
const referrerPolicy = require('referrer-policy')
const csp = require('helmet-csp')
const redisStore = require('connect-redis')(session)
const moment = require('moment')
// --------------Models ------------------
const Users = require('./models/users')
// --------------ROUTES--------------------
const index = require('./routes/index');
const user = require('./routes/user');
const prizes = require('./routes/prizes');
const earncoins = require('./routes/earncoins');
const profile = require('./routes/profile');
var RateLimit = require('express-rate-limit');
// app.enable('trust proxy'); // only if you're behind a reverse proxy (Heroku, Bluemix, AWS if you use an ELB, custom Nginx setup, etc)
// apply to all requests
// var mongodburl = process.env.MONGODB_URI ||"mongodb://{$process.env.DB_USER}:{$process.env.DB_PASS}@ds151024.mlab.com:51024/freerewards"
// var hbs = expressHbs.create({
// // Specify helpers which are only registered on this instance.
// helpers: {
// foo: function () { return 'FOO!'; },
// bar: function () { return 'BAR!'; }
// }
// });
//initialization
require('dotenv').config()
var mongodburl = process.env.mongodb || 'mongodb://localhost:27017/freereward'
var MongoClient = require('mongodb').MongoClient;
var client = redis.createClient();
var csrfProtection = csrf({ cookie: true })
const options = {
useMongoClient: true,
autoIndex: false, // Don't build indexes
reconnectTries: Number.MAX_VALUE, // Never stop trying to reconnect
reconnectInterval: 500, // Reconnect every 500ms
poolSize: 10, // Maintain up to 10 socket connections
// If not connected, return errors immediately rather than waiting for reconnect
bufferMaxEntries: 0
};
var limiter = new RateLimit({
windowMs:5*60*1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
delayMs: 0 ,// disable delaying - full speed until the max limit is reached
message: "Too many requests"
});
mongoose.connect(mongodburl,options);
//-----------------BEGIN-----------------
var app = express();
app.use(helmet());
app.use(referrerPolicy({ policy: 'no-referrer' }))
// app.use(csp({
// directives: {
// defaultSrc: ["'self'"],
// styleSrc: ["'self'", 'maxcdn.bootstrapcdn.com','fonts.googleapis.com','https://fonts.gstatic.com/s/quicksand/v7/6xKtdSZaM9iE8KbpRA_hJFQNcOM.woff2','https://use.fontawesome.com/c3b6c7d70e.css'],
// scriptSrc: ["'self'",'use.fontawesome.com','unpkg.com','google.com','unpkg.com','https://cdn.fontawesome.com/js/stats.js','https://use.fontawesome.com/c3b6c7d70e.css'],
// fontSrc:["'self'",'https://fonts.gstatic.com/s/quicksand/v7/6xKtdSZaM9iE8KbpRA_hJFQNcOM.woff2']
// }
// }))
// view engine setup
app.engine('.hbs', expressHbs({ defaultLayout: 'layout', extname: '.hbs' }));
app.set('view engine', '.hbs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon_nwg_icon.ico')));
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(compression())
var Hours = 3600000 * 5
app.use(
session({
secret: process.env.sessionSecret,
store:new redisStore({host:'localhost',port:6379,client: client}),
//,client: client,ttl : 260
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: true,
// secure: true,
expires : new Date(Date.now() + Hours),
maxAge : Hours
}
})
);
app.use(expressValidator({
customValidators:{
isEqaul:function(value,value2){
return value == value2
},
hasNumAndChar:function(value){
var regex = /^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/;
return regex.test(value);
}
},
errorFormatter: function (param, msg, value) {
var namespace = param.split('.')
, root = namespace.shift()
, formParam = root;
while (namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
param: formParam,
msg: msg,
value: value
};
}
}));
app.use(passport.initialize());
app.use(passport.session())
app.use(express.static(path.join(__dirname, 'public')));
app.use(flash());
app.enable('trust proxy')
app.use(csrfProtection)
//------------Global VARIABLES-------------------------
app.use(function (req, res, next) {
//console.log(req.headers['x-forwarded-for'])
//console.log(req.ip)
res.locals.success= req.flash('success');
res.locals.errors = req.flash('error');
res.locals.user = req.user || null;
res.locals.csrfToken = req.csrfToken()
if (req.user) {
res.locals.logged = true;
res.locals.getDaily = false
let now = moment(moment().format('DD/MM/YYYY hh:mm'), 'DD/MM/YYYY hh:mm')
let last = moment(moment(req.user.lastdailybonus, 'DD/MM/YYYY hh:mm').format('DD/MM/YYYY hh:mm'), 'DD/MM/YYYY hh:mm')
if (moment.duration(now.diff(last)).asHours() >= 24) {
res.locals.getDaily =true
}
res.locals.user =
{
_id: req.user._id,
name: req.user.name,
email: req.user.email,
username: req.user.username,
coins: req.user.coins,
joindate: req.user.joindate,
lastdailybonus: req.user.lastdailybonus,
__v: req.user.__v,
completedMissions: req.user.completedMissions,
orders: req.user.orders,
profileimgurl:req.user.profileimgurl
}
// let refUserCoins = 0;
// for(let i = 0; i < req.user.refferedUsers.length;i ++){
// refUserCoins += req.user.refferedUsers[i].coins
// }
res.locals.usercoins = Math.floor(req.user.totalCoins)
} else {
res.locals.logged = false;
}
next();
});
// app.use(limiter);
//----------------SET ROUTES----------------
app.use('/', index);
app.use('/user' ,user);
app.use('/prizes', prizes);
app.use('/earncoins', earncoins);
app.use('/profile', profile);
// catch 404 and forward to error handler
app.use(function (error,req, res, next) {
var err = new Error(error);
console.log(err.stack)
err.status = 404;
next(err);
});
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
if (err.code !== 'EBADCSRFTOKEN') return next(err)
if(err.code === 'LIMIT_FILE_SIZE'){res.redirect('/erorooorroro')}
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;