-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
197 lines (170 loc) · 5.62 KB
/
index.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import express from "express";
import mongoose from 'mongoose';
import bodyParser from "body-parser";
//for image upload
import multer from "multer";//for file upload
import path from "path";//to get path of file
import _ from "lodash";//while using array to store img we compare title and send it to
import {Post} from "./postModel.js"; // Import the Mongoose model
import {config} from "dotenv"
config();
const MongoUrl = process.env.MongoUrl;
// Get the directory path of the current module
const currentModulePath = new URL(import.meta.url).pathname;
// Set the views directory
const viewsPath = path.join(path.dirname(currentModulePath), 'views');
mongoose.connect(MongoUrl, {
useNewUrlParser: true,
useUnifiedTopology: true,
writeConcern: { w: 'majority' }
}).then(()=>console.log("MongoDB connected"))
.catch((err)=>console.log("not connected"));
const app=express();
// Set the views directory
app.set('views', viewsPath);
const port=process.env.PORT;
app.set("view engine","ejs");
//app.set("views", "D:\\Web Development\\Blog website\\views");
//app.set("views", "D:\\Web Development\\Blog website\\views");
app.use(express.static("public"));
// app.get("/",(req,res)=>{
// res.render("index.ejs");
// });
app.get("/", async (req, res) => {
try {
const posts = await Post.find({}); // Fetch all posts from the database
res.render("index.ejs", { posts: posts }); // Pass the posts to the view
} catch (error) {
console.error("Error fetching posts:", error);
res.render("index.ejs", { posts: [] });
}
});
app.get("/contacts",(req,res)=>{
res.render("contacts.ejs");
});
app.get("/about", (req, res) => {
res.render("about.ejs");
});
app.post("/blog1",(req,res)=>{
res.render("blog1.ejs");
});
app.post("/blog2",(req,res)=>
{
res.render("blog2.ejs");
});
app.post("/blog3",(req,res)=>{
res.render("blog3.ejs");
});
// //for img upload by using array to store new blogs
// let posts=[];
// const storage = multer.diskStorage({
// destination: function (req, file, cb) {
// cb(null, "public/uploads/");
// },
// filename: function (req, file, cb) {
// cb(null, Date.now() + "_" + path.extname(file.originalname));
// },
// });
// const upload = multer({ storage: storage });
// app.post("/newpage", upload.single("postImage"), function (req, res) {
// const post = {
// image: req.file ? req.file.filename : "",
// title: req.body.postTitle,
// content: req.body.postBody,
// };
// posts.push(post);
// res.redirect("/")
// });
// app.get("/newpage", (req, res) => {
// res.render("newpage.ejs");
// });
// // post page
// app.get("/posts/:postTitle", (req, res) => {
// const { postTitle } = req.params;
// const requestedPost = posts.find((post) => _.lowerCase(post.title) === _.lowerCase(postTitle));
// if (requestedPost) {
// res.render("posts", { post: requestedPost });
// } else {
// res.render("error", { errorMessage: "Post not found" });
// }
// });
// app.post("/posts/:postTitle/delete", (req, res)=> {
// const requestedTitle = req.params.postTitle;
// const postIndex = posts.findIndex(function (post) {
// return _.lowerCase(post.title) === _.lowerCase(requestedTitle);
// });
// if (postIndex !== -1) {
// posts.splice(postIndex, 1);
// res.redirect("/");
// } else {
// res.render("error", {
// errorMessage: "Post not found"
// });
// }
// });
// app.get("/", (req, res)=> {
// res.render("/", {
// startingContent: homeStartingContent,
// posts: posts
// });
// });
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "public/uploads/");
},
filename: function (req, file, cb) {
cb(null, Date.now() + "_" + path.extname(file.originalname));
},
});
const upload = multer({ storage: storage });
// ... Your other imports and code ...
app.get("/newpage", (req, res) => {
res.render("newpage.ejs");
});
// ... The rest of your code ...
// Replace your /newpage POST route to save the post data to the database
app.post("/newpage", upload.single("postImage"), async function (req, res) {
const post = new Post({
image: req.file ? req.file.filename : "",
title: req.body.postTitle,
content: req.body.postBody,
});
try {
await post.save();
res.redirect("/");
} catch (error) {
// Handle error if the post saving fails
console.error("Error saving the post:", error);
res.redirect("/newpage");
}
});
// Replace your homepage route to fetch the posts from the database
//pos
app.get("/posts/:postTitle", async(req, res) => {
const { postTitle } = req.params;
try{
const requestedPost = await Post.findOne({title:postTitle});
if (requestedPost) {
res.render("posts", { post: requestedPost });
} else {
res.render("error", { errorMessage: "Post not found" });
}
}catch (error) {
// Handle error if the database query fails
console.error("Error fetching the post:", error);
res.render("error", { errorMessage: "Error fetching the post" });
}
});
app.post("/posts/:postTitle/delete", async(req, res)=> {
const requestedTitle = req.params.postTitle;
try{
await Post.findOneAndDelete({title: requestedTitle});
res.redirect("/");
} catch(error){
console.error("Error deleting the post:",error)
res.render("error",{errorMessage:"Error deleting the post"});
}
});
app.listen(port,(req,res)=>{
console.log(`Server started at ${port}`)
});