-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblogapp.js
158 lines (114 loc) · 5.6 KB
/
blogapp.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const _ = require("lodash");
const mongoose = require("mongoose");
const methodOverride = require('method-override');
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
app.use(methodOverride('_method'));
mongoose.connect("mongodb://localhost:27017/blogDB", {useNewUrlParser: true, useUnifiedTopology: true});
let posts = [
{ title: "The Rise of Chatbots", content: "In the age of AI, chatbots have become our digital companions. Discover how these clever bots are changing the way we interact online, from providing customer support to engaging in casual conversations. Join the chatbot revolution!" },
{ title: "Journey to the Red Planet", content: "Embark on a cosmic adventure as we delve into the latest updates on Mars exploration. From the perseverance of rovers to the dreams of human colonization, we explore the mysteries of the Red Planet. Buckle up, space enthusiasts!" },
{ title: "The Unstoppable Comeback", content: "Witness the heart-stopping moments of the greatest sports comebacks in history. From the underdog stories to the miraculous turnarounds, we celebrate the resilience and determination that define these epic sporting moments." },
{ title: "Coding: The Art of Problem-Solving", content: "Dive into the world of programming, where lines of code transform into elegant solutions. Explore the art of problem-solving, the joy of debugging, and the thrill of crafting software that makes an impact. Coding is not just a skill; it's an art form." },
{ title: "Navigating the Digital Jungle", content: "In the vast digital jungle of social media, discover the latest trends, pitfalls, and success stories. From viral memes to impactful campaigns, we explore the ever-evolving landscape of online interaction. Grab your virtual binoculars and join the expedition!" }
];
const homeStartingContent = "Welcome to our Blog App!"
// Create a schema for blog posts
const postSchema = {
title: String,
content: String,
date: {
type: Date,
default: Date.now
}
};
// Create a model based on the schema
const Post = mongoose.model("Post", postSchema);
// Other routes and logic will be added later
app.get('/', async (req, res) => {
try {
const posts = await Post.find().sort({ date: 'desc' });
res.render('home', { startingContent: homeStartingContent, posts: posts });
} catch (err) {
console.error(err);
res.status(500).send('Internal Server Error');
}
});
app.get('/about', (req, res) => {
// Descriptive text about the blog
const aboutContent = "Welcome to The CyberJungle Blog, your go-to source for a wild ride through the realms of technology, AI, space exploration, sports, programming, and social media. Our team of passionate writers is dedicated to bringing you captivating stories and insights from the digital jungle. Join us on this adventure into the future!"
// Render the about view and pass the about content
res.render('about', { aboutContent: aboutContent });
});
app.get('/contact', (req, res) => {
// Render the contact view
const contactContent = "If you want to contact me, here is the information below:"
res.render('contact', { contactContent: contactContent });
});
app.get('/create', (req, res) => {
res.render('makeposts');
});
app.post('/create', async (req, res) => {
const postTitle = req.body.postTitle;
const postBody = req.body.postBody;
const date = new Date();
const formattedDate = `${date.toLocaleDateString()} ${date.toLocaleTimeString([], {hour: '2-digit', minute:'2-digit', second: '2-digit'})}`;
const post = new Post({
title: postTitle,
content: postBody,
date: formattedDate
});
try {
await post.save();
res.redirect('/');
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error");
}
});
app.get('/edit/:postId', async (req, res) => {
try {
const postId = req.params.postId;
const post = await Post.findById(postId);
// Render the edit form template and pass the post details
res.render('edit', { post: post });
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error");
}
});
// POST route to handle form submission for editing a post
app.post('/edit/:postId', async (req, res) => {
try {
const postId = req.params.postId;
const updatedTitle = req.body.postTitle;
const updatedContent = req.body.postBody;
// Find the post by ID and update the title and content
await Post.findByIdAndUpdate(postId, { title: updatedTitle, content: updatedContent });
res.redirect('/');
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error");
}
});
app.delete('/delete/:postId', async (req, res) => {
try {
const postId = req.params.postId;
// Find the post by ID and delete it
await Post.findByIdAndDelete(postId);
res.redirect('/');
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error");
}
});
// Start the server
const port = 3000; // You can change the port as needed
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});