Guest User

Untitled

a guest
Apr 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. var express = require("express"),
  2. app = express(),
  3. bodyParser = require("body-parser"),
  4. mongoose = require("mongoose"),
  5. Campground = require("./models/campground"),
  6. seedDB = require("./seeds");
  7.  
  8. seedDB();
  9.  
  10. //connecting to mongo and naming database as yelp_camp
  11. mongoose.connect("mongodb://localhost/yelp_camp_3");
  12. app.use(bodyParser.urlencoded({extended: true}));
  13. app.set("view engine", "ejs");
  14.  
  15.  
  16. app.get("/", function(req, res){
  17. res.render("landing");
  18. });
  19.  
  20. //INDEX - show all campgrounds
  21. app.get("/campgrounds", function(req, res){
  22. Campground.find({}, function(err, allCampgrounds){
  23. if(err)
  24. console.log("Error");
  25. else
  26. res.render("index", {campGrounds: allCampgrounds});
  27. });
  28. });
  29.  
  30. //CREATE - add new campgrounds to database
  31. app.post("/campgrounds", function(req, res){
  32. //gettting data from form
  33. var name = req.body.name;
  34. var image = req.body.image;
  35. var desc = req.body.description;
  36. var newCampGround = {name: name, image: image, description: desc};
  37. Campground.create(newCampGround, function(err, newlyCreated){
  38. if(err)
  39. console.log("Error");
  40. else
  41. res.redirect("/campgrounds");
  42. })
  43. });
  44.  
  45. //NEW - show form to create new campground
  46. app.get("/campgrounds/new", function(req, res){
  47. res.render("new.ejs");
  48. });
  49.  
  50. // //SHOW - show more info about one campground
  51. // app.get("/campgrounds/:id", function(req, res){
  52. // Campground.findById(req.params.id).populate("comments").exec(function(err, foundCampground){
  53. // if(err){
  54. // console.log(err);
  55. // } else {
  56. // console.log(foundCampground);
  57. // res.render("show", {campground: foundCampground});
  58. // }
  59. // });
  60. // });
  61.  
  62. // SHOW - shows more info about one campground
  63. app.get("/campgrounds/:id", function(req, res){
  64. //find the campground with provided ID
  65. Campground.findById(req.params.id).populate("comments").exec(function(err, foundCampground){
  66. if(err){
  67. console.log(err);
  68. } else {
  69. console.log(foundCampground)
  70. //render show template with that campground
  71. res.render("show", {campground: foundCampground});
  72. }
  73. });
  74. });
  75.  
  76.  
  77. app.listen(3000, function(){
  78. console.log("YelpCamp Server Started!");
  79. });
  80.  
  81. var mongoose = require("mongoose");
  82.  
  83. //Schema Setup
  84. var campgroundSchema = new mongoose.Schema({
  85. name: String,
  86. image: String,
  87. description: String,
  88. comments: [
  89. {
  90. type: mongoose.Schema.Types.ObjectId,
  91. ref: "Comment" //Name of model
  92. }
  93. ]
  94. });
  95.  
  96. module.exports = mongoose.model("Campground", campgroundSchema);
  97.  
  98. var mongoose = require("mongoose");
  99.  
  100. var commentSchema = mongoose.Schema({
  101. text: String,
  102. author: String
  103. });
  104.  
  105. module.exports = mongoose.model("Comment", commentSchema);
  106.  
  107. <% include partials/header %>
  108.  
  109. <h1><%= campground.name %></h1>
  110.  
  111. <img src="<%= campground.image %>">
  112.  
  113. <p><%= campground.description %></p>
  114.  
  115. <% include partials/footer %>
Add Comment
Please, Sign In to add comment