-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
119 lines (86 loc) · 2.44 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
//jshint esversion:8
import path from 'path';
const __dirname = path.resolve();
import express from "express";
import bodyParser from "body-parser";
import _ from "lodash";
import dotenv from "dotenv";
dotenv.config();
import mongoose from 'mongoose';
const app = express();
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static("public"));
let port = 3000;
const aboutContent = "THIS IS JUST A BASIC JOURNAL SITE, YOU CAN STORE YOUR BASIC JOURNALS HERE";
const contactContent = "CONTACT ME PAGE.";
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
main().catch(err => console.log(err));
async function main() {
await mongoose.connect(process.env.URL);
}
const postSchema = {
title: String,
content: String
};
const Post = mongoose.model("Post", postSchema);
app.get("/", (req, res) => {
Post.find({}, function(err, posts){
if (!err) {
res.render("home", {
posts: posts
});
}
});
});
app.get("/contact", (req, res) => {
res.render("contact", {contactContent: contactContent});
});
app.get("/about", (req, res) => {
res.render("about", {aboutContent: aboutContent});
});
app.get("/compose", (req, res) => {
res.render("compose");
});
// LOADING THE POST PAGE URL USING THE URL OF THE POST
app.get("/posts/:postId", function(req, res){
const requestedPostId = req.params.postId;
Post.findOne({_id: requestedPostId}, (err, post) => {
res.render("post", {newPostTitle: post.title, newPostText: post.content});
});
});
app.get("/delete/:postId", function(req, res) {
const requestedPostId = req.params.postId;
Post.findByIdAndRemove({_id: requestedPostId}, (err, doc) => {
if (!err) {
res.redirect('/');
} else {
console.log('Failed to Delete user Details: ' + err);
}
});
});
app.get("/deleteAll", function(req, res) {
// const requestedPostId = req.params.postId;
Post.deleteMany({}, (err, doc) => {
if (!err) {
res.redirect('/');
} else {
console.log('Failed to Delete All user Details: ' + err);
}
});
});
app.post("/compose", (req,res) => {
const post = new Post ({
title: req.body.journalTitle,
content: req.body.journalText
});
post.save(function(err){
if (!err){
res.redirect("/");
}
});
});
app.listen(process.env.PORT, () => {
console.log(`Server started on port ${process.env.PORT}`);
});