Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require("express"),
- app = express(),
- bodyParser = require("body-parser"),
- mongoose = require("mongoose"),
- Campground = require("./models/campground"),
- seedDB = require("./seeds");
- seedDB();
- //connecting to mongo and naming database as yelp_camp
- mongoose.connect("mongodb://localhost/yelp_camp_3");
- app.use(bodyParser.urlencoded({extended: true}));
- app.set("view engine", "ejs");
- app.get("/", function(req, res){
- res.render("landing");
- });
- //INDEX - show all campgrounds
- app.get("/campgrounds", function(req, res){
- Campground.find({}, function(err, allCampgrounds){
- if(err)
- console.log("Error");
- else
- res.render("index", {campGrounds: allCampgrounds});
- });
- });
- //CREATE - add new campgrounds to database
- app.post("/campgrounds", function(req, res){
- //gettting data from form
- var name = req.body.name;
- var image = req.body.image;
- var desc = req.body.description;
- var newCampGround = {name: name, image: image, description: desc};
- Campground.create(newCampGround, function(err, newlyCreated){
- if(err)
- console.log("Error");
- else
- res.redirect("/campgrounds");
- })
- });
- //NEW - show form to create new campground
- app.get("/campgrounds/new", function(req, res){
- res.render("new.ejs");
- });
- // //SHOW - show more info about one campground
- // app.get("/campgrounds/:id", function(req, res){
- // Campground.findById(req.params.id).populate("comments").exec(function(err, foundCampground){
- // if(err){
- // console.log(err);
- // } else {
- // console.log(foundCampground);
- // res.render("show", {campground: foundCampground});
- // }
- // });
- // });
- // 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).populate("comments").exec(function(err, foundCampground){
- if(err){
- console.log(err);
- } else {
- console.log(foundCampground)
- //render show template with that campground
- res.render("show", {campground: foundCampground});
- }
- });
- });
- app.listen(3000, function(){
- console.log("YelpCamp Server Started!");
- });
- var mongoose = require("mongoose");
- //Schema Setup
- var campgroundSchema = new mongoose.Schema({
- name: String,
- image: String,
- description: String,
- comments: [
- {
- type: mongoose.Schema.Types.ObjectId,
- ref: "Comment" //Name of model
- }
- ]
- });
- module.exports = mongoose.model("Campground", campgroundSchema);
- var mongoose = require("mongoose");
- var commentSchema = mongoose.Schema({
- text: String,
- author: String
- });
- module.exports = mongoose.model("Comment", commentSchema);
- <% include partials/header %>
- <h1><%= campground.name %></h1>
- <img src="<%= campground.image %>">
- <p><%= campground.description %></p>
- <% include partials/footer %>
Add Comment
Please, Sign In to add comment