-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
115 lines (104 loc) · 2.96 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
require('dotenv').config()
var express = require("express")
var app = express()
var bodyParser = require("body-parser")
var mongoose = require("mongoose")
var Photos = require("./models/photos")
var Comment = require("./models/comment")
var User = require("./models/user")
var multer = require("multer")
var storage = multer.diskStorage({
filename: function(req, file, cb){
cb(null, Date.now() + file.originalname)
}
});
var imageFilter = function (req, file, cb) {
// accept image files only
if (!file.originalname.match(/\.(jpg|jpeg|png|gif)$/i)) {
return cb(new Error('Only image files are allowed!'), false);
}
cb(null, true);
};
var upload = multer({ storage: storage, fileFilter: imageFilter})
var cloudinary = require('cloudinary');
cloudinary.config({
cloud_name: 'iiitians-network',
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET
});
mongoose.connect("mongodb://localhost/reflection", { useNewUrlParser: true, useUnifiedTopology: true}).then(() => {
console.log('Connected to DB!')
}).catch(err => {
console.log('ERROR:', err.message)
});
app.set("view engine", "ejs")
app.use(express.static(__dirname+"/public"))
// app.use("/uploads", express.static("uploads"))
app.use(bodyParser.urlencoded({ extended: true }))
//*******//
// Home //
app.get("/", function(req, res){
res.render("home")
})
//***********//
// Gallery //
//***********//
app.get("/gallery", function(req, res){
Photos.find({}, function(err, photos){
res.render("photos", {gallery:photos})
})
})
app.get("/gallery/create", function(req, res){
res.render("new")
})
//Show//
app.get("/gallery/:id", function(req, res){
Photos.findById(req.params.id).populate("comment").exec(function(err, photos){
if(err){
console.log(err)
res.redirect("back")
}else{
res.render("show", {gallery:photos})
}
})
})
//New//
app.post("/gallery", upload.single('image'), function(req, res){
cloudinary.uploader.upload(req.file.path, function(result) {
// add cloudinary url for the image to the campground object under image property
req.body.galleries.image = result.secure_url
Photos.create(req.body.galleries, function(err, photos){
if(err){
console.log(err)
res.redirect("back")
}else{
res.redirect("/gallery")
}
})
})
})
//************//
// Comments //
//************//
app.post("/gallery/:id/comments", function(req, res){
Photos.findById(req.params.id, function(err, photos){
if(err){
console.log(err)
res.redirect("back")
}else{
Comment.create(req.body.comments, function(err, comments){
if(err){
console.log(err)
res.redirect("/")
}else{
photos.comment.push(comments)
photos.save()
res.redirect("/gallery/"+req.params.id)
}
})
}
})
})
app.listen(3000, function(){
console.log("Server listening to Reflection")
})