-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
235 lines (202 loc) · 7.03 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
var express= require("express");
var app= express();
var bodyparser =require("body-parser");
var mongoose = require("mongoose");
var passport = require("passport");
var LocalStrategy =require("passport-local");
var User = require("./models/user");
var methodOverride = require("method-override");
var flash =require("connect-flash");
var dotenv = require('dotenv').config(); // for managing environment variables stored in .env file
var Campground = require("./models/campground");
var seedDB = require("./seeds");
var Comment = require("./models/comment");
//seedDB();
app.use(flash());
//Requiring Routes
var commentRoutes =require("./routes/comments"),
campgroundRoutes = require("./routes/campgrounds"),
indexRoutes = require("./routes/index")
//PASSPORT CONFIGURATION
app.use(require("express-session")({
secret:"Charizrad",
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());
app.use((req,res,next)=>{
res.locals.currentuser=req.user;
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
next();
});
// local mongodb setup
//mongoose.connect("mongodb://localhost:27017/yelpcamp",{ useNewUrlParser: true, useUnifiedTopology: true });
const url = process.env.DATABASEURL || "mongodb://localhost:27017/yelpcamp";
// Mongodb Atlas Setup (Cloud)
mongoose.connect(url,
{ useNewUrlParser: true, useUnifiedTopology: true
}).then( ()=>{
console.log(url);
console.log("Connected to DB!");
}).catch( err=>{
console.log("Error :",err.message);
});
app.use(bodyparser.urlencoded({extended:true}));
app.use(express.static(__dirname+"/public"));
app.set("view engine","ejs") ;
app.use(methodOverride("_method"));
//,(error,response,body)=>{})
//schema setup
// var campgroundSchema =new mongoose.Schema({
// title : String,
// image : String,
// description : String,
// });
// var Campground =mongoose.model("Campground",campgroundSchema);
//CREATE - Create new campgrounds
// Campground.create(
// {title :"ooty", image:"http://tiny.cc/y5vdoz",description: "WELCOME TO INDIA",},
// function(err,campground){
// if(err){
// console.log(err);
// }else{
// console.log("Newly created campground");
// console.log(campground);
// }
// });
app.get("/",(req,res)=>{
res.render("landing");
});
var campgrounds =[
{title :"ooty", image:"http://tiny.cc/y5vdoz",description: "WELCOME TO INDIA",},
{title :"ladhak", image:"http://tiny.cc/3vvdoz",description: "WELCOME TO INDIA",},
{title :"kutch", image:"http://tiny.cc/x0vdoz",description: "WELCOME TO INDIA",},
{title :"manali", image:"http://tiny.cc/g3vdoz",description: "WELCOME TO INDIA",}
];
// //INDEX -show all campgrounds
// app.get("/campgrounds",(req,res)=>{
// //get all campgrounds from db
// Campground.find({},function(err,allcampgrounds){
// if (err){console.log(err)}
// else{res.render("campgrounds/index",{anyname : allcampgrounds}); //here anyname can literally be given any name including campgrounds}
// }
// });
// });
// app.post("/campgrounds",(req,res)=>{
// //get data from form and add to campgrounds array
// var name =req.body.title;
// var image =req.body.image;
// var desc = req.body.description;
// var newcampground ={title: name,image:image, description :desc};
// //Dont need anymore-campgrounds.push(newcampground);
// //Create a new campground and save to DB
// Campground.create(newcampground,function(err,newlycreated){
// if(err){console.log(err);}
// else{ res.redirect("/campgrounds"); } //redirect back to campgrounds page
// });
// });
// // NEW - show form to create new campgrounds
// app.get("/campgrounds/new",(req,res)=>{
// res.render("campgrounds/new.ejs");
// });
// //SHOW-show info about specific ..
// app.get("/campgrounds/:id",(req,res)=>{
// //res.send("This will be the show page one day");
// //find the campground by provided id
// Campground.findById(req.params.id).populate("comments").exec((err,foundcampground)=>{
// if(err){
// console.log(err);}
// else{
// console.log(foundcampground);
// res.render("campgrounds/show.ejs",{campground : foundcampground});
// }
// });
// });
// ///=======================================================================================================
// // COMMENTS ROUTES
// ///=======================================================================================================
// app.get("/campgrounds/:id/comments/new",isLoggedIn,(req,res)=>{
// Campground.findById(req.params.id,(err,campground)=>{
// if(err){
// console.log(err);
// }
// else{
// res.render("comments/new",{campground:campground});
// }
// });
// });
// app.post("/campgrounds/:id/comments",isLoggedIn,(req,res)=>{
// //lookup campground using ID
// Campground.findById(req.params.id,(err,campground)=>{
// if(err){
// console.log(err);
// res.redirect("/campgrounds");
// }
// else{
// Comment.create(req.body.comment,(err,comment)=>{
// if(err){console.log(err);}
// else{
// campground.comments.push(comment);
// campground.save();
// res.redirect("/campgrounds/"+campground._id);
// }
// });
// }
// });
// });
// //========================
// //AUTH ROUTES
// //=========================
// // SHOW REGISTER FORM
// app.get("/register",(req,res)=>{
// res.render("register");
// });
// app.post("/register",(req,res)=>{
// var newUser = new User({username:req.body.username});
// User.register(newUser,req.body.password,(err,user)=>{
// if(err){
// console.log(err);
// return res.render("register");
// }
// passport.authenticate("local")(req,res,()=>{
// res.redirect("/campgrounds")
// });
// });
// });
// //show login form
// app.get("/login",(req,res)=>{
// res.render("login");
// })
// //app.post("login",middleware,callback();
// app.post("/login",passport.authenticate("local",
// {
// successRedirect:"/campgrounds",
// failureRedirect : "/login"
// }),(req,res)=>{
// });;
// //logout route
// app.get("/logout",(req,res)=>{
// req.logout();
// res.redirect("/campgrounds");
// });
// //MIDDLE WARE FUNCTION FOR CHECKING IF USER IS LOGGED IN
// function isLoggedIn(req,res,next){
// if(req.isAuthenticated()){
// return next();
// }
// res.redirect("/login");
// }
app.use(indexRoutes);
app.use(campgroundRoutes);
app.use(commentRoutes);
const port = process.env.PORT || 5000;
const ip = process.env.IP || "0.0.0.0";
app.listen(port,ip ,()=>{
console.log("THe Yelpcamp server Has started");
console.log(`Server is running at http://${ip}:${port}`);
});