forked from Kritika30032002/Blog_Website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
161 lines (138 loc) · 5.11 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
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
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const _ = require("lodash");
require("dotenv").config();
const mongoose = require("mongoose");
const session = require("express-session");
const passport = require("passport");
const authRouter = require("./routers/auth");
const contactRouter = require("./routers/contact");
const { User } = require("./models/auth");
const blogsData=require('./blog.json')
const blogs1=blogsData.blogs
const fs = require('fs');
const homeStartingContent =
"Step into the world of words with our user-friendly blog platform! Whether you're a seasoned writer or just getting started, our intuitive interface makes composing and editing blogs a breeze! Join our community of storytellers, where your unique voice is celebrated. Ready to share your thoughts? Click the 'Compose' button below and let your creativity flow! Your blogging journey begins here.";
const aboutContent =
"Welcome to Daily Journal, your go-to destination for daily journaling and blogging. We understand the power of words and the importance of sharing your thoughts with the world. Our platform is designed to empower individuals like you to express themselves, reflect on their daily lives, and connect with a community of like-minded writers.";
const contactContent =
"Scelerisque eleifend donec pretium vulputate sapien. Rhoncus urna neque viverra justo nec ultrices. Arcu dui vivamus arcu felis bibendum. Consectetur adipiscing elit duis tristique. Risus viverra adipiscing at in tellus integer feugiat. Sapien nec sagittis aliquam malesuada bibendum arcu vitae. Consequat interdum varius sit amet mattis. Iaculis nunc sed augue lacus. Interdum posuere lorem ipsum dolor sit amet consectetur adipiscing elit. Pulvinar elementum integer enim neque. Ultrices gravida dictum fusce ut placerat orci nulla. Mauris in aliquam sem fringilla ut morbi tincidunt. Tortor posuere ac ut consequat semper viverra nam libero.";
const app = express();
const PORT = process.env.PORT || 3000;
const sessionConfig = {
name: "session",
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
cookie: {
httpOnly: true,
expires: Date.now() + 1000 * 60 * 60 * 24 * 7,
maxAge: 1000 * 60 * 60 * 24 * 7,
},
};
app.set("view engine", "ejs");
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.use(express.static("public"));
app.use(session(sessionConfig));
app.use(express.json());
app.use(passport.initialize());
app.use(passport.session());
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
let posts = [];
app.use((req, res, next) => {
res.locals.user = req.user;
next();
});
app.get("/", function (req, res) {
res.render("home", {
blog1: homeStartingContent,
posts: [],
});
});
app.get("/home", function (req, res) {
res.render("home2", {
blog1: homeStartingContent,
posts: posts,
});
});
app.get("/about", function (req, res) {
res.render("about", {
blog2: aboutContent,
});
});
app.get("/contact", function (req, res) {
res.render("contact", {
blog3: contactContent,
});
});
app.get("/compose", function (req, res) {
res.render("compose");
});
app.post("/compose", function (req, res) {
const post = {
title: req.body.newTitle,
blog: req.body.newBlog,
image: req.body.newImage,
};
const post1 = {
title: req.body.newTitle,
content: req.body.newBlog,
image: req.body.newImage,
};
posts.push(post);
blogsData.blogs.push(post1)
const jsonString = JSON.stringify(blogsData, null, 2);
fs.writeFileSync('./blog.json', jsonString, 'utf-8');
res.redirect("/home");
});
app.get("/posts/:postName", function (req, res) {
const requestedTitle = _.lowerCase(req.params.postName);
posts.forEach(function (post) {
const storedTitle = _.lowerCase(post.title);
if (storedTitle === requestedTitle) {
res.render("post", {
nextBlogTitle: post.title,
nextBlog: post.blog,
});
}
});
});
app.get("/blogs", function (req, res) {
res.render("blogs",{blogs: blogsData.blogs});
});
app.get("/error", function (req, res) {
res.render("error",{error1: "Unauthorised Access"});
});
app.get("/blogs/:blogName", function (req, res) {
const requestedTitle = _.lowerCase(req.params.blogName);
blogsData.blogs.forEach(function (blog) {
const storedTitle = _.lowerCase(blog.title);
if (storedTitle === requestedTitle) {
res.render("post", {
nextBlogImage: blog.image,
nextBlogTitle: blog.title,
nextBlogAuthor: blog.author,
nextBlog: blog.content,
});
}
});
});
app.use("/", authRouter);
app.use("/", contactRouter);
app.get("*", (req, res) => {
res.status(404).render("404");
});
mongoose
.connect(process.env.MONGODB_URI)
.then(() => console.log("connected"))
.catch((error) => console.log(error));
app.listen(PORT, function () {
console.log(`Server started on port ${PORT}`);
});