-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
67 lines (49 loc) · 2.9 KB
/
app.js
File metadata and controls
67 lines (49 loc) · 2.9 KB
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
var express = require("express"),
mongoose = require("mongoose"),
Campground = require("./models/campground.js"),
Comment = require("./models/comment.js"),
User = require("./models/user.js"),
bodyParser = require("body-parser"),
passport = require("passport"),
localStrategy = require("passport-local"),
passportLocalMongoose = require("passport-local-mongoose"),
seedDB = require("./seeds.js"),
methodOverride = require("method-override"),
flash = require("connect-flash");
var campgroundRoutes = require("./routes/campground.js"),
commentRoutes = require("./routes/comment.js"),
indexRoutes = require("./routes/index.js");
var app = express();
app.set("view engine","ejs");
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static(__dirname+"/public"));//this will define the whole path to public directory
app.use(methodOverride("_method"));
app.use(flash());//this use should come before passport configuration
app.use(require("express-session")({
secret : "Rusty is the cutest dog ever",//this is basically used to decode and encode..
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new localStrategy(User.authenticate()));//this will tell passport to use that version of authentication that has been sent by the UserSchema
passport.serializeUser(User.serializeUser());//it encodes the data from the session
passport.deserializeUser(User.deserializeUser());//it decodes the and sends the data to the session
app.use(function(req,res,next){ //whatever function we put in app.use is provided to all the routes
res.locals.currentUser = req.user;//what ever variable we put in locals is provided in every template so currentUser variable can be used in all the templates having the value of req.user=> means username and user id.....
res.locals.error = req.flash("error");//this will pass on messsage variable to every ejs page you have not to do it separately
res.locals.success = req.flash("success");
next();
});//req.user is empty if no user is logged in else it will contain a username and an id
//mongoose.connect("mongodb://localhost/yelp_camp",{ useNewUrlParser: true });
var URL = process.env.DATABASEURL || "mongodb://localhost/yelp_camp";
mongoose.connect(URL,{ useNewUrlParser: true });
//mongodb://<dbuser>:<dbpassword>@ds155294.mlab.com:55294/yelp_camp
//seedDB();//in Database seeding data is provided to database when it is being installed
//CRUD...............................................................................................
app.use("/",indexRoutes);
app.use("/campgrounds",campgroundRoutes); //it will tell that all the routes in campgroundRoutes.js starts from /campgrounds
app.use("/campgrounds/:id/comments",commentRoutes);
app.listen(process.env.PORT,process.env.IP,function(){
console.log("YelpCamp server has been started...");
});