-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
103 lines (81 loc) · 3.85 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
require('dotenv').config();
var express = require("express"); //the framework
var app = express(); // renaming express as app
var bodyParser = require("body-parser"); //use to transfer data from the frontend to the backend
var mongoose = require("mongoose"); //mongoose handles the database
var flash = require("connect-flash"); //handles the alert/flash messages on the app
var passport = require("passport"); //authentification
var LocalStrategy = require("passport-local"); //auth
var methodOverride = require("method-override"); //use to add functionality to html like the PUT and DELETE method in html forms
var Campground = require("./models/campground"); //require MongoDB database Schemas
var Comment = require("./models/comment"); //require MongoDB database Schemas
var User = require("./models/user"); //require MongoDB database Schemas
var seedDB = require("./seeds"); //seeding the app with fake data for testing
//Requiring App Routes
var commentRoutes = require("./routes/comments"),
reviewRoutes = require("./routes/reviews"),
campgroundsRoutes = require("./routes/campgrounds"),
authRoutes = require("./routes/index");
//mongoose.connect('mongodb://localhost:27017/yelp_camp_v14', { useNewUrlParser: true });
mongoose.connect('mongodb://localhost:27017/yelp_camp_v15', {useNewUrlParser: true, useCreateIndex: true});
//Seeding the site with data for testing
// seedDB();
//Use CSS file inside the Public Directory
app.use(express.static(__dirname + "/public"));
//Body-parser is used to collect data from the body and convert it to JS
app.use(bodyParser.urlencoded({extended: true}));
//use to ommit .ejs at the end of ejs files name
app.set("view engine", "ejs");
//Method override to include PUT and Update method in HTML FORM
app.use(methodOverride("_method"));
//Using package alert/flash messages
app.use(flash());
//Moment to tell the time since something has been added
app.locals.moment = require('moment');
//PASSPORT CONFIGURATION
app.use(require("express-session")({
secret: "I hope there is season 4 news at EVO Japan!",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
//Pass req.user = currentUser to every route using middleware(shortcut)
// app.use(async function(req, res, next){
// res.locals.currentUser = req.user;
// if(req.user) {
// try {
// let user = await User.findById(req.user._id).populate('notifications', null, { isRead: false }).exec();
// res.locals.notifications = user.notifications.reverse();
// } catch(err) {
// console.log(err.message);
// }
// }
// res.locals.error = req.flash("error");
// res.locals.success = req.flash("success");
// next();
// });
app.use(async function(req, res, next){
res.locals.currentUser = req.user;
if(req.user) {
try {
let user = await User.findById(req.user._id).populate('notifications', null, { isRead: false }).exec();
res.locals.notifications = user.notifications.reverse();
} catch(err) {
console.log(err.message);
}
}
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
next();
});
app.use("/", authRoutes);
app.use("/campgrounds", campgroundsRoutes);
app.use("/campgrounds/:id/comments", commentRoutes);
app.use("/campgrounds/:id/reviews", reviewRoutes);
app.listen(process.env.PORT, process.env.IP, function(){
console.log("The YelpCamp Server has started");
});