-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
90 lines (78 loc) · 2.52 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
var express = require("express"),
app = express(),
bodyParser = require("body-parser"),
mongoose = require("mongoose")
mongoose.connect("mongodb://localhost/yelp_camp");
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");
// SCHEMA SETUP
var campgroundSchema = new mongoose.Schema({
name: String,
image: String,
description: String
});
var Campground = mongoose.model("Campground", campgroundSchema);
// Campground.create(
// {
// name: "Granite Hill",
// image: "https://farm1.staticflickr.com/60/215827008_6489cd30c3.jpg",
// description: "This is a huge granite hill, no bathrooms. No water. Beautiful granite!"
// },
// function(err, campground){
// if(err){
// console.log(err);
// } else {
// console.log("NEWLY CREATED CAMPGROUND: ");
// console.log(campground);
// }
// });
app.get("/", function(req, res){
res.render("landing");
});
//INDEX - show all campgrounds
app.get("/campgrounds", function(req, res){
// Get all campgrounds from DB
Campground.find({}, function(err, allCampgrounds){
if(err){
console.log(err);
} else {
res.render("index",{campgrounds:allCampgrounds});
}
});
});
//CREATE - add new campground to DB
app.post("/campgrounds", function(req, res){
// get data from form and add to campgrounds array
var name = req.body.name;
var image = req.body.image;
var desc = req.body.description;
var newCampground = {name: name, image: image, description: desc}
// Create a new campground and save to DB
Campground.create(newCampground, function(err, newlyCreated){
if(err){
console.log(err);
} else {
//redirect back to campgrounds page
res.redirect("/campgrounds");
}
});
});
//NEW - show form to create new campground
app.get("/campgrounds/new", function(req, res){
res.render("new.ejs");
});
// SHOW - shows more info about one campground
app.get("/campgrounds/:id", function(req, res){
//find the campground with provided ID
Campground.findById(req.params.id, function(err, foundCampground){
if(err){
console.log(err);
} else {
//render show template with that campground
res.render("show", {campground: foundCampground});
}
});
})
app.listen(process.env.PORT, process.env.IP, function(){
console.log("The YelpCamp Server Has Started!");
});