-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
98 lines (84 loc) · 2.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
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
mongoose.connect("mongodb://localhost:27017/yelp_camp", {useNewUrlParser: true, useUnifiedTopology: true});
app.use(bodyParser.urlencoded({extended:true}));
app.set("view engine", "ejs");
var campgroundSchema=new mongoose.Schema({
name: String,
image: String,
description: String
});
var Campground = mongoose.model("Campground",campgroundSchema);
Campground.create(
{
name:"camp 2",
image:"https://media-cdn.tripadvisor.com/media/vr-splice-j/07/3b/86/f3.jpg",
description:"This is very beautiful and classic campground."
},function(err,campground){
if(err)
console.log("Something Went Wrong!!!");
else{
console.log("Added Object:");
console.log(campground);
}
}
);
/*
var campgrounds=[
{name:"camp 1",image:"https://www.travelbirbilling.com/wp-content/uploads/Bir-Billing4.jpg"},
{name:"camp 2",image:"https://media-cdn.tripadvisor.com/media/vr-splice-j/07/3b/86/f3.jpg"},
{name:"camp 3",image:"https://www.travelbirbilling.com/wp-content/uploads/camp-Oak-View.jpg"}
];
*/
app.get("/",function(req,res){
res.render('landing');
});
app.post("/campgrounds",function(req,res){
var name = req.body.name;
var image = req.body.image;
var desc = req.body.description;
var newcamp = {name:name, image:image, description:desc};
Campground.create(newcamp, function(err,camp){
if(err)
{
console.log(err);
}
else
{
res.redirect("/campgrounds");
}
});
});
app.get("/campgrounds/new",function(req,res){
res.render("newcamp");
});
app.get("/campgrounds",function(req,res){
//get all campgrounds from db
Campground.find({},function(err,campgrounds){
if(err)
{
console.log(err);
}
else{
res.render("campgrounds",{campgrounds:campgrounds});
}
});
});
app.get("/campgrounds/:id",function(req,res){
var campid = req.params.id;
Campground.findById(campid,function(err,showcamp){
if(err)
{
console.log(err);
}
else
{
res.render("show",{showcamp:showcamp});
}
});
});
app.listen(3000,function(){
console.log("YelpCamp server has started!");
});