-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
122 lines (99 loc) · 3.25 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
116
117
118
119
120
121
122
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');
const ejs = require('ejs');
const multer = require('multer');
const utilities = require(__dirname + "/utilities.js");
const BlogPost = require(__dirname + "/models/blog.js");
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static('public'));
const mongoose = require('mongoose');
mongoose.connect("Connection URL", {useNewUrlParser: true, useUnifiedTopology: true});
const homeStartingContent = "Welcome to the home page. View your recent activity below.";
const aboutContent = "This is a Node and Express application to help you get started with building a blog of your own";
const contactContent = "I can be reached at dhruvseth7@berkeley.edu if you have any questions";
let storage = multer.memoryStorage();
let upload = multer({storage: storage});
app.get("/", (req, res) => {
BlogPost.find((err, posts) => {
if (posts) {
res.render("home", {home: homeStartingContent, blogPosts: posts});
}
})
})
app.get("/about", (req, res) => {
res.render("about", {about: aboutContent});
})
app.get("/contact", (req, res) => {
res.render("contact", {contact: contactContent});
})
app.get("/compose", (req, res) => {
res.render("compose");
})
app.post("/compose", upload.single("uploadedImage"), (req, res) => {
const title = req.body.title;
const content = req.body.post;
let image = req.body.image;
if (!image || image === "") {
image = "gridimage-" + utilities.getRandomInt(6).toString() + ".jpeg";
}
var newPost;
if (!req.file) {
newPost = new BlogPost({
title: title,
content: content,
postedDate: utilities.getDate(),
image: image
})
} else {
newPost = new BlogPost({
title: title,
content: content,
postedDate: utilities.getDate(),
imageFile: {
data: req.file.buffer,
contentType: 'image/png'
}
})
}
newPost.save().then(() => res.redirect("/"));
})
app.get("/posts/:postId", (req, res) => {
let postId = req.params.postId;
BlogPost.findOne({_id: postId}, (err, doc) => {
if (doc) {
res.render("post", {post: doc});
}
})
})
app.get("/delete/:deleteId", (req, res) => {
let deleteId = req.params.deleteId;
BlogPost.deleteOne({_id: deleteId}, (err) => {
if (!err) {
res.redirect("/");
}
})
})
app.get("/update/:updateId", (req, res) => {
let updateId = req.params.updateId;
BlogPost.findOne({_id: updateId}, (err, doc) => {
res.render("update", {title: doc.title, content: doc.content, id: updateId});
})
})
app.post("/update", (req, res) => {
const updateId = req.body.id;
const updatedTitle = req.body.title;
const updatedContent = req.body.post;
BlogPost.updateOne({_id: updateId}, {
title: updatedTitle,
content: updatedContent,
postedDate: utilities.getDate()
}).then(() => res.redirect("/"));
})
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Listening on port ${port}`);
})