-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
70 lines (46 loc) · 2.18 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
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const passport = require("passport");
const localStrategy = require("passport-local");
const methodOverride = require('method-override');
// Importing files
var Campground = require("./models/campground.js"); // ./ - references the current directory
var Comment = require("./models/comment.js");
var User = require("./models/user.js");
var seedDB = require("./seeds.js");
var authRoutes = require("./routes/index.js");
var campgroundRoutes = require("./routes/campgrounds.js");
var commentRoutes = require("./routes/comments.js");
mongoose.connect("mongodb://localhost/yelp_camp_12", { useNewUrlParser: true }); // connected to yelp_camp_v7 database
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(__dirname + "/public")); // __dirname - directory where script was run
app.use(methodOverride("_method"));
// seedDB(); seed db
// Passport config
app.use(require("express-session")({
secret: "Top secret page", // secret - used to encode and decode sessions
resave: false,
saveUninitialized: false
}));
// always needed when using passport
app.use(passport.initialize());
app.use(passport.session());
passport.use(new localStrategy(User.authenticate()));
// reading the data from the session, then encoding and decoding the sessions
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use((req, res, next) => {
res.locals.currentUser = req.user; // res.locals.currentUser - enables currentUser variable to be used on all templates and routes, req.user - contains logged-in users username and password
next(); // compulary as it will enable to move to next middleware/route handler
});
// Refactoring
app.use("/", authRoutes);
app.use("/campgrounds", campgroundRoutes); // adds /campgrounds as a prefix to every route in campgrounds.js
app.use("/campgrounds/:id/comments", commentRoutes);
//edit camp
//update camp
app.listen(3000, function(){ // process.env.PORT, process.env.IP - environmental viriables set up for cloud9 which we access
console.log("Server started");
});