-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
66 lines (55 loc) · 2.51 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
const express = require("express"),
app = express(),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
flash = require("connect-flash"),
Campground = require("./models/campground"),
Comment = require("./models/comment"),
User = require("./models/user"),
passport = require("passport"),
localStrategy = require("passport-local"),
methodOverride= require("method-override");
seedDB = require("./seeds");
// Requiring Routes
const indexRoutes = require("./routes/index"),
commentRoutes = require("./routes/comments"),
campgroundRoutes = require("./routes/campgrounds");
//creating env variable for DATABASE URL using cmd: export DATABASEURL=mongodb://localhost:27017/yelp_camp
//url from environment variable + adding backup in case env. variable lost,
//env variable will be different for heroku(i.e. used to switch bwn different databases for development & production env)
var url = process.env.DATABASEURL || "mongodb://localhost:27017/yelp_camp";
mongoose.connect(url, {useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology:true, useFindAndModify: false});
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(__dirname + "/public"));
app.use(methodOverride("_method"));
app.use(flash());
// seedDB(); //seed the database
// Passport Config
app.use(require("express-session")({
secret: "Kaamehaamehaaa",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
// to use currentUser in ALL routes... using app.use (middleware)
app.use(function(req, res, next){
// whatever we put in res.locals is available in all templates...here currentUser
res.locals.currentUser = req.user;
// here error and success are keys that contain flash msg in its value
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
next(); //next, since it is a middleware
});
// Using Routes
app.use(indexRoutes);
app.use("/campgrounds/:id/comments", commentRoutes);
app.use("/campgrounds", campgroundRoutes); // /campgrounds will be add as a prefix to all campground routes
passport.use(new localStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
// Listening
app.listen(process.env.PORT || 80, function(){
console.log("The YelpCamp SERVER has started!");
});