-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
115 lines (96 loc) · 3.45 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
const express = require('express'),
app = express(),
bodyParser = require('body-parser'),
flash = require('connect-flash'),
mongoose = require('mongoose'),
Project = require('./models/project'),
Comment = require('./models/comment'),
Rating = require('./models/rating'),
Ministry = require('./models/ministry'),
Suggestion = require('./models/suggestion'),
MinistryKey = require('./models/ministryKey'),
State = require('./models/states'),
passport = require('passport'),
LocalStrategy = require('passport-local'),
User = require('./models/user'),
request = require('request');
methodOverride = require('method-override');
keys = require('./config/keys');
session = require('express-session');
helmet = require('helmet');
MongoStore = require('connect-mongo')(session);
seedModoner = require('./config/seedModoner');
//requiring routes
var commentRoutes = require('./routes/comments'),
projectRoutes = require('./routes/projects'),
indexRoutes = require('./routes/index'),
ministryRoutes = require('./routes/ministries'),
suggestionRoutes = require('./routes/suggestions'),
stateRoutes = require('./routes/states'),
budgetRoutes = require('./routes/budgets');
mongoose.connect(keys.db_address, { useMongoClient: true });
//express session
app.use(session({
secret : "Sessionals suck",
name : "sessionId",
store : new MongoStore({mongooseConnection : mongoose.connection}),
resave : false,
saveUninitialized : false
}));
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({extended : true}));
app.use(methodOverride("_method"));
app.set('view engine', 'ejs');
app.use(flash());
app.use(helmet());
seedModoner(); //seed the database
//Passport configuration
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
//passing currentuser data to every route
var pr;
var projectPostedBy;
Project.find({}, function(err, projects){
if(err){
console.log("Something went wrong!");
}else{
pr = projects;
}
});
Project.distinct("ministryName", function(err, ministryNames){
if(err){
console.error(err);
}else{
projectPostedBy = ministryNames;
}
})
app.use(function(req, res, next){
res.locals.currentUser = req.user;
res.locals.projects = pr;
res.locals.projectPostedBy = projectPostedBy;
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
next();
});
app.use("/", indexRoutes);
app.use("/projects", projectRoutes);
app.use("/projects/:id/comments", commentRoutes);
app.use('/ministry', ministryRoutes);
app.use('/suggestions', suggestionRoutes);
app.use('/states', stateRoutes);
app.use('/budgets', budgetRoutes);
app.use(function(req, res) {
res.status(400);
res.render('error', {code : "404 Not found"});
});
app.use(function(err, req, res, next){
// console.error(err);
res.status(500);
res.render('error', {code : "500 Internal Server Error"});
});
app.listen(process.env.PORT, process.env.IP, function(){
console.log("Server Deployed");
});