AlistairWallace
6/28/2017 - 3:22 AM

Sample Node using Mongo Connection

Sample Node using Mongo Connection

var express     = require("express");
var app         = express();
var bodyparser  = require("body-parser");
var mongoose    = require("mongoose");
// Turns on Body parser
app.use(bodyparser.urlencoded({extended: true}));

// Tells Express that we are using ejs files in the views folder
app.set("view engine", "ejs");


mongoose.connect("mongodb://localhost/yelp_camp"); // Creates the DB if it does not exist under the name yelp_camp....
// Schema Setup
var campgroundsSchema = new mongoose.Schema({
        name: String,
        image: String,
        description: String
});

var CampGround = mongoose.model("CampGround", campgroundsSchema);


// Create dummy record
//  CampGround.create(
//          {
//           name: "Camp logo",
//           image: "http://thespringscamp.com/wp-content/uploads/2010/11/summer-camp-border-clipart-14909_2.gif",
//           description: "this is My Description"

//          }, function (err, campground) {

//              if (err) {
//                  console.log(err);

//              } else {
//                  console.log("Created Camp");
//                  console.log(campground);
//              }
//          });




//Build a router for root paths
app.get("/", function (req, res) {

    res.render("landing");
    // body...
});


// Build Router for CampGrounds
// INDEX
app.get("/campgrounds", function (req, res) {

  //Get Data from DB
  CampGround.find({}, function (err, allCampGrounds) { // NOTE allCampGrounds is the call back variable created right here - Could be any name

      if (err) {
          console.log(err);
      } else {
          res.render("index", {campgrounds: allCampGrounds});
      };
  })

//User comes in with the /campgrounds route
// data gets built and the user gets passed to the campgrounds.ejs page..
});


//Collecting a POSTED in a new campground
//update DB
app.post("/campgrounds", function (req, res) {
    // Get data from form
    var name = req.body.name;
    var image = req.body.image;
    var desc = req.body.description;

    // Add data to current Array
    var newcampground = {name: name, image: image, description: desc};

    // Create record in DB from Posting in
    CampGround.create(newcampground, function (err, newlyCreated) {

        if (err) {
            console.log(err);
        } else {
            //Redirect to campgrounds once done adding to DB
            res.redirect("/campgrounds");        }
    });
} );

// create a form page
app.get("/campgrounds/new", function(req, res) {
    res.render("newcampground");
})


app.get("/campgrounds/:id", function(req, res) {

    CampGround.findById(req.params.id, function(err, foundCampground) {

        if (err) {
            console.log(err);
        } else {
            // Go to show.ejs
            res.render("show", {campground: foundCampground});
        }
    })
});



//Last function
app.listen(process.env.PORT, process.env.IP, function () {
    // Tell me the app is running..
    console.log("app is running");
})