-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
139 lines (110 loc) · 4.16 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
/** ==========================================
; Title: fundstrtr donate
; Description: Donation Platform
; Author: Oyindamola Obaleke
; Date: 7 Dec 2020
;=======================================*/
require('dotenv').config()
var express = require('express')
var bodyParser = require('body-parser'),
mongoose = require('mongoose')
passport = require('passport'),
LocalStrategy = require('passport-local')
passportLocalMongoose = require('passport-local-mongoose'),
fileSystem = require('fs'),
methodOverride = require('method-override'),
flash = require('connect-flash')
RateLimit = require('express-rate-limit');
const fileupload = require('express-fileupload');
//Rate Limiter
//max 5 requests per minuter-prevent denial-of-service attack
var limiter = new RateLimit({
windowMs: 1*60*1000, // (60,000ms) 1 minute
max: 5
});
//Production -ScaleGrid
var certificateFileBuf = fileSystem.readFileSync("sslCA");
var options = {
sslCA: certificateFileBuf
}
// mongoose.connect(process.env.MongoDbScaleGrid, options)
// .then(() => {
// console.log("Connected to MongoDbAtlas")
// }).catch(function(err) {
// console.log("Error" + err)
// })
//Production-Atlas
// mongoose.connect(process.env.MongoDBAtlas, {
// useNewUrlParser: true,
// useUnifiedTopology: true
// }).then(() => {
// console.log("Connected to MongoDbAtlas")
// }).catch(function(err) {
// console.log("Error" + err)
// })
//Development
mongoose.connect('mongodb://localhost/fundstrtr_donate_app', { useNewUrlParser: true, useUnifiedTopology: true });
var app = express()
app.use(express.json())
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(__dirname + "/public"));
app.use(methodOverride("_method")) //whenever app gets a request having _method use that new request to override
app.use(limiter);
app.set("view engine", "ejs");
//MODELS
var donatees = require('./models/investmentopportunities')
var discussion = require('./models/discussion')
var User = require('./models/user')
//ROUTES DECLARATION
var homeRoute = require('./controllers/homeController/homeController')
var investmentOppRoutes = require('./controllers/investmentOppController/invOppController')
let raisingRoutes = require('./controllers/raisingController/raisingController')
let commentsRoutes = require('./controllers/commentsController/commentsController')
let authenticationRoutes = require('./controllers/authController/authenticationController')
let userProfileRoute = require('./controllers/userProfileController/userProfileController')
//FLASH MESSAGE
app.use(flash());
//PASSPORT CONFIG
app.use(require('express-session')({ //requiring the package and passing in some options
secret: "ezffst", //used to encode and decode the sessions
resave: false,
saveUninitialized: false
}))
app.use(fileupload({ useTempFiles: true }));
//set passport up for use
app.use(passport.initialize())
app.use(passport.session())
//--Responsible for reading the session & taking the data thats encoded in session and unencoding it
// encoding the data and put back in session
passport.serializeUser(User.serializeUser());
//unencoding the data
passport.deserializeUser(User.deserializeUser())
//passport.use(new LocalStrategy(User.authenticate())) //uses default passport config, username & password
passport.use(User.createStrategy())
//Middleware- passing currentUser and Flash messages to every route
app.use(function(req, res, next) {
res.locals.currentUser = req.user
res.locals.successMessage = req.flash('success_message');
res.locals.errorMessage = req.flash('error_message');
next();
})
//ROUTES
app.use(homeRoute)
app.use(authenticationRoutes)
app.use(userProfileRoute)
app.use(investmentOppRoutes)
app.use(raisingRoutes)
app.use(commentsRoutes)
//HANDLE 404 REQUEST
app.use(function(req, res) {
//render error page
res.status(404).render('ErrorPage/404');
});
//Production -only runs on port 3000 on servers- .process.env.PORT, process.env.IP,
// app.listen(3000, function() {
// console.log("Fundstrtr listening on port 3000")
// })
//Development
app.listen("3002", function() {
console.log("donate listening on port 3002")
})