-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 62ab273
Showing
27 changed files
with
2,531 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
var express = require("express"), | ||
app = express(), | ||
bodyParser = require("body-parser"), | ||
mongoose = require("mongoose"), | ||
passport = require("passport"), | ||
cookieParser = require("cookie-parser"), | ||
LocalStrategy = require("passport-local"), | ||
flash = require("connect-flash"), | ||
Campground = require("./models/campground"), | ||
Comment = require("./models/comment"), | ||
User = require("./models/user"), | ||
session = require("express-session"), | ||
seedDB = require("./seeds"), | ||
methodOverride = require("method-override"); | ||
// configure dotenv | ||
require('dotenv').load(); | ||
|
||
//requiring routes | ||
var commentRoutes = require("./routes/comments"), | ||
campgroundRoutes = require("./routes/campgrounds"), | ||
indexRoutes = require("./routes/index") | ||
|
||
// assign mongoose promise library and connect to database | ||
mongoose.Promise = global.Promise; | ||
|
||
const databaseUri = process.env.MONGODB_URI || 'mongodb://localhost/yelp_camp'; | ||
|
||
mongoose.connect(databaseUri, { useMongoClient: true }) | ||
.then(() => console.log(`Database connected`)) | ||
.catch(err => console.log(`Database connection error: ${err.message}`)); | ||
|
||
app.use(bodyParser.urlencoded({extended: true})); | ||
app.set("view engine", "ejs"); | ||
app.use(express.static(__dirname + "/public")); | ||
app.use(methodOverride('_method')); | ||
app.use(cookieParser('secret')); | ||
//require moment | ||
app.locals.moment = require('moment'); | ||
// seedDB(); //seed the database | ||
seedDB(); | ||
|
||
|
||
// PASSPORT CONFIGURATION | ||
app.use(require("express-session")({ | ||
secret: "Once again Rusty wins cutest dog!", | ||
resave: false, | ||
saveUninitialized: false | ||
})); | ||
|
||
app.use(flash()); | ||
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; | ||
res.locals.success = req.flash('success'); | ||
res.locals.error = req.flash('error'); | ||
next(); | ||
}); | ||
|
||
|
||
app.use("/", indexRoutes); | ||
app.use("/campgrounds", campgroundRoutes); | ||
app.use("/campgrounds/:id/comments", commentRoutes); | ||
|
||
const port = process.env.PORT || 3000; | ||
const ip = process.env.IP; | ||
|
||
app.listen(port, ip, function(){ | ||
console.log(`The YelpCamp Server Has Started! on ${ip} : ${port}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
var Comment = require('../models/comment'); | ||
var Campground = require('../models/campground'); | ||
module.exports = { | ||
isLoggedIn: function(req, res, next){ | ||
if(req.isAuthenticated()){ | ||
return next(); | ||
} | ||
req.flash('error', 'You must be signed in to do that!'); | ||
res.redirect('/login'); | ||
}, | ||
checkUserCampground: function(req, res, next){ | ||
Campground.findById(req.params.id, function(err, foundCampground){ | ||
if(err || !foundCampground){ | ||
console.log(err); | ||
req.flash('error', 'Sorry, that campground does not exist!'); | ||
res.redirect('/campgrounds'); | ||
} else if(foundCampground.author.id.equals(req.user._id) || req.user.isAdmin){ | ||
req.campground = foundCampground; | ||
next(); | ||
} else { | ||
req.flash('error', 'You don\'t have permission to do that!'); | ||
res.redirect('/campgrounds/' + req.params.id); | ||
} | ||
}); | ||
}, | ||
checkUserComment: function(req, res, next){ | ||
Comment.findById(req.params.commentId, function(err, foundComment){ | ||
if(err || !foundComment){ | ||
console.log(err); | ||
req.flash('error', 'Sorry, that comment does not exist!'); | ||
res.redirect('/campgrounds'); | ||
} else if(foundComment.author.id.equals(req.user._id) || req.user.isAdmin){ | ||
req.comment = foundComment; | ||
next(); | ||
} else { | ||
req.flash('error', 'You don\'t have permission to do that!'); | ||
res.redirect('/campgrounds/' + req.params.id); | ||
} | ||
}); | ||
}, | ||
isAdmin: function(req, res, next) { | ||
if(req.user.isAdmin) { | ||
next(); | ||
} else { | ||
req.flash('error', 'This site is now read only thanks to spam and trolls.'); | ||
res.redirect('back'); | ||
} | ||
}, | ||
isSafe: function(req, res, next) { | ||
if(req.body.image.match(/^https:\/\/images\.unsplash\.com\/.*/)) { | ||
next(); | ||
}else { | ||
req.flash('error', 'Only images from images.unsplash.com allowed.\nSee https://youtu.be/Bn3weNRQRDE for how to copy image urls from unsplash.'); | ||
res.redirect('back'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
var mongoose = require("mongoose"); | ||
|
||
var campgroundSchema = new mongoose.Schema({ | ||
name: String, | ||
image: String, | ||
description: String, | ||
state: String, | ||
party: String, | ||
chamber: String, | ||
general: Number, | ||
primary: Number, | ||
lat: Number, | ||
long: Number, | ||
createdAt: { type: Date, default: Date.now }, | ||
author: { | ||
id: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "User" | ||
}, | ||
username: String | ||
}, | ||
comments: [ | ||
{ | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "Comment" | ||
} | ||
] | ||
}); | ||
|
||
module.exports = mongoose.model("campground", campgroundSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
var mongoose = require("mongoose"); | ||
|
||
var commentSchema = mongoose.Schema({ | ||
text: String, | ||
amount: Number , | ||
campaignID : Number, | ||
createdAt: { type: Date, default: Date.now }, | ||
author: { | ||
id: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "User" | ||
}, | ||
username: String | ||
} | ||
}); | ||
|
||
module.exports = mongoose.model("Comment", commentSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
var mongoose = require("mongoose"); | ||
var passportLocalMongoose = require("passport-local-mongoose"); | ||
|
||
var UserSchema = new mongoose.Schema({ | ||
username: String, | ||
password: String, | ||
isAdmin: {type: Boolean, default: false}, | ||
address: { | ||
street1: String, | ||
street2: String, | ||
city: String, | ||
state: String, | ||
zip: String | ||
}, | ||
profession: String, | ||
employer: String | ||
}); | ||
|
||
UserSchema.plugin(passportLocalMongoose) | ||
|
||
module.exports = mongoose.model("User", UserSchema); |
Oops, something went wrong.