Here is the error that I’m getting while run node app.js
,
Here is how my directories look like,
Here is my app.js file,
var express = require('express'),
app = express(),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
passport = require("passport"),
LocalStrategy = require("passport-local"),
methodOverride = require("method-override"),
Campground = require("./models/campground"),
Comment = require("./models/comment"),
User = require("./models/user"),
seedDB = require("./seeds");
//requiring routes
var commentRoutes = require("./routes/comments"),
campgroundRoutes = require("./routes/campgrounds"),
indexRoutes = require("./routes/index")
mongoose.connect("mongodb://localhost:27017/yelp_camp_v8",{useNewUrlParser: true, useUnifiedTopology: true});
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(methodOverride("_method"));
// seedDB(); //seed the database
//PASSPORT CONFIGURATION
app.use(require("express-session")({
secret: "Once again Rusty wins cutest dog!",
resave: false,
saveUninitialized: false
}))
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(function(req, res, next){
res.locals.currentUser = req.user;
next();
});
app.use("/",indexRoutes);
app.use("/campgrounds",campgroundRoutes);
app.use("/campgrounds/:id/comments",commentRoutes);
app.listen(3000,function(){
console.log("The YelpCamp Server has started!!");
});
Here is my index.js file,(from middleware directory)
//all the middleware goes here
var middlewareObj = {};
middlewareObj.checkCamgroundOwnership = function(req, res, next){
if(req.isAuthenticated()){
Campground.findById(req.params.id, function(err, foundCampground){
if(err){
res.redirect("back");
} else {
//does user own the campground?
if(foundCampground.author.id.equals(req.user._id)){
next();
} else {
res.redirect("back");
}
}
});
} else {
res.redirect("back");
}
}
middlewareObj.checkCommentOwnership = function(req, res, next){
if(req.isAuthenticated()){
Comment.findById(req.params.comment_id, function(err, foundComment){
if(err){
res.redirect("back");
} else {
//does user own the comment?
if(foundComment.author.id.equals(req.user._id)){
next();
} else {
res.redirect("back");
}
}
});
} else {
res.redirect("back");
}
}
middlewareObj.isLoggedIn = function isLoggedIn(req, res, next){
if(req.isAuthenticated()){
return next();
}
res.redirect("/login");
}
module.exports = middlewareObj;
Here is my camgrounds.js file,
var express = require("express");
var router = express.Router();
var Campground = require("../models/campground");
var middleware = require("../middleware");
router.get("/", function(req, res){
//Get all campgrounds from DB
Campground.find({}, function(err,allCampgrounds){
if(err){
console.log("There is an ERROR!");
console.log(err);
}else{
res.render("./campgrounds/index",{campgrounds:allCampgrounds});
}
});
});
//CREATE - add new campground to DB
router.post("/", middleware.isLoggedIn, function(req, res){
//GET DATA FROM FORM AND ADD TO CAMPGOUNDS ARRAY
var name = req.body.name;
var image = req.body.image;
var desc = req.body.description;
var author = {
id: req.user._id,
username: req.user.username
}
var newCampground = {name: name, image: image, description: desc, author: author};
//Create a new campground and save to database
Campground.create(newCampground, function(err,new_item){
if(err){
console.log("There is an error!");
console.log(err);
}else{
//REDIRECT BACK TO CAMPROUNDS PAGE
res.redirect("/campgrounds");
}
});
});
//NEW - show form to create new campground
router.get("/new", middleware.isLoggedIn, function(req, res){
res.render("campgrounds/new");
});
// SHOW - shows more info about campground
router.get("/: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("There is an ERROR!");
console.log(err);
}else{
console.log(foundCampground);
//render show templete with that campground
res.render("campgrounds/show",{campground: foundCampground});
}
});
});
//EDIT CAMPGROUND ROUTE
router.get("/:id/edit", middleware.checkCampgroundOwnership, function(req, res){
Campground.findById(req.params.id, function(err, foundCampground){
res.render("campgrounds/edit", {campground: foundCampground});
});
});
//UPDATE CAMPGROUND ROUTE
router.put("/:id", middleware.checkCampgroundOwnership, function(req, res){
//find and update the correct campground
Campground.findByIdAndUpdate(req.params.id, req.body.campground, function(err, updatedCampground){
if(err){
res.redirect("/campgrounds");
} else {
res.redirect("/campgrounds/" + req.params.id);
}
});
//redirect somewhere(show page)
});
//DESTROY CAMPGROUND ROUTE
router.delete("/:id", middleware.checkCampgroundOwnership, function(req, res){
Campground.findByIdAndRemove(req.params.id, function(err){
if(err){
res.redirect("/campgrounds");
} else{
res.redirect("/campgrounds");
}
});
});
module.exports = router;
Here is my comments.js file,
var express = require("express");
var router = express.Router({mergeParams: true});
var Campground = require("../models/campground");
var Comment = require("../models/comment");
var middleware = require("../middleware");
//Comments New
router.get("/new", middleware.isLoggedIn, function(req, res){
// find Campground by id
Campground.findById(req.params.id, function(err, campground){
if(err){
console.log(err);
} else{
res.render("comments/new",{campground: campground});
}
})
});
//Comments Create
router.post("/", middleware.isLoggedIn, function(req, res){
//lookup campground using ID
Campground.findById(req.params.id,function(err, campground){
if(err){
console.log(err)
res.redirect("/campgrounds");
} else{
//create new comment
Comment.create(req.body.comment, function(err, comment){
if(err){
console.log(err);
} else{
//add username and id to comment
comment.author.id = req.user._id;
comment.author.username = req.user.username;
//save comment
comment.save();
//connect new comment to campground
campground.comments.push(comment);
campground.save();
//redirect to campground show page
res.redirect('/campgrounds/' + campground._id);
}
});
}
});
});
//COMMENT EDIT ROUTE
router.get("/:comment_id/edit", middleware.checkCommentOwnership, function(req, res){
Comment.findById(req.params.comment_id, function(err, foundComment){
if(err){
res.redirect("back");
}else{
res.render("comments/edit", {campground_id: req.params.id, comment: foundComment});
}
});
});
//COMMENT UPDATE
router.put("/:comment_id", middleware.checkCommentOwnership, function(req, res){
Comment.findByIdAndUpdate(req.params.comment_id, req.body.comment, function(err, updatedComment){
if(err){
res.redirect("back");
} else {
res.redirect("/campgrounds/" + req.params.id);
}
});
});
//COMMENT DESTROY ROUTE
router.delete("/:comment_id", middleware.checkCommentOwnership, function(req, res){
//findByIdAndRemove
Comment.findByIdAndRemove(req.params.comment_id, function(err){
if(err){
res.redirect("back");
} else {
res.redirect("/campgrounds/" + req.params.id);
}
});
});
module.exports = router;
Please let me know where I’m making mistakes?