-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
214 lines (191 loc) · 5.2 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const dotenv = require("dotenv");
dotenv.config();
const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const session = require("express-session");
const mongoDBstore = require("connect-mongodb-session")(session);
const app = express();
const postHandler = require("./models/posts");
const userHandler = require("./models/users");
const store = new mongoDBstore({
uri: process.env.CONNECTIONSTRING,
collection: "sessions",
});
app.set("view engine", "ejs");
app.set("views", "views");
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, "public")));
app.use(
session({
secret: "mySecretLOL",
resave: false,
saveUninitialized: false,
store: store,
cookie: { expires: new Date(253402300000000) },
})
);
app.use((req, res, next) => {
res.locals.isLoggedin = req.session.isLoggedin || false;
next();
});
app.get("/login", (req, res, next) => {
res.render("login");
});
app.post("/login", userHandler.login);
app.get("/register", (req, res, next) => {
res.render("registration", { message: "" });
});
app.post("/register", (req, res, next) => {
if (req.session.isLoggedin) {
res.redirect("/showPost");
}
const username = req.body.shownName;
const email = req.body.username;
const password = req.body.passw;
userHandler.findUser(email, (user) => {
if (!user) {
userHandler.saveUser(email, password, username, () => {
res.redirect("/addPost");
});
} else {
res.render("registration", { message: "user already registered" });
}
});
});
app.use("/logout", (req, res, next) => {
req.session.destroy((err) => {
console.error(err);
});
res.redirect("addPost");
});
app.get("/addPost", (req, res, next) => {
if (!req.session.isLoggedin) {
return res.redirect("/login");
}
console.log(req.session.isLoggedin);
res.render("form");
});
app.post("/addPost", (req, res, next) => {
const user = req.session.user.username;
const title = req.body.headline;
const story = req.body.story;
const tags = req.body.tags.split(",");
postHandler.save(title, story, user, tags, () => res.redirect("/addPost"));
});
app.get("/post/:postId", (req, res, next) => {
const id = req.params.postId;
postHandler.disOne(id, (data) => {
res.render("showOne", { data: data });
});
});
app.get("/showPost", (req, res, next) => {
const page = +req.query.page || 1;
postHandler.dis(page, (data, page, nums) => {
const prevPage = +page - 1;
const nextPage = +page + 1;
const numPages = Math.ceil(nums / 10);
res.render("show", {
data: data,
currentPage: page,
prevPage: prevPage,
nextPage: nextPage,
numPages: numPages,
});
});
});
app.post("/addComment", (req, res, next) => {
if (!req.session.isLoggedin) {
return res.redirect("/login");
}
const idPost = req.body.postId;
const comment = req.body.comm;
console.log(idPost);
console.log(comment);
postHandler.addComm(idPost, comment, () => {
res.redirect("/post/" + idPost);
});
});
app.post("/upvote", (req, res, next) => {
const idPost = req.body.postId;
console.log(idPost);
postHandler.upvote(idPost, () => {
res.status(200).json({ message: "success" });
});
});
app.post("/downvote", (req, res, next) => {
const idPost = req.body.postId;
console.log(idPost);
postHandler.downvote(idPost, () => {
res.status(200).json({ message: "success" });
});
});
app.get("/reset", (req, res) => {
if (req.session.isLoggedin) {
return res.redirect("/login");
}
res.render("reset");
});
app.post("/reset", (req, res, next) => {
const email = req.body.email;
userHandler.reset(email, () => {
res.redirect("/login");
});
});
app.get("/resetPassword/:token", (req, res, next) => {
const token = req.params.token;
userHandler.rsr(
token,
() => {
res.redirect("/login");
},
(user) => {
res.render("resetForm", { email: user.email, token: user.token });
}
);
});
app.post("/resetPassword", (req, res) => {
const email = req.body.email;
const token = req.body.token;
const password = req.body.password;
console.log(email);
console.log(token);
const cb = () => {
res.redirect("/login");
};
const errcb = () => {
res.redirect("/register");
};
userHandler.newPassword(email, token, password, cb, errcb);
});
let tag = "";
app.use("/search", (req, res, next) => {
console.log(req.body.tag);
tag = req.body.tag || tag;
const page = +req.query.page || 1;
postHandler.search(tag, page, (data, page, nums) => {
const prevPage = +page - 1;
const nextPage = +page + 1;
const numPages = Math.ceil(nums / 10);
res.render("show", {
data: data,
currentPage: page,
prevPage: prevPage,
nextPage: nextPage,
numPages: numPages,
});
});
});
app.get("/", (req, res, next) => {
const isLoggedin = req.session.isLoggedin;
if (!isLoggedin) {
return res.render("home.ejs");
} else {
const user = req.session.user;
res.render("search", { user: user });
}
});
app.listen(process.env.PORT || 3000, function () {
console.log("SERVER STARTED PORT: 3000");
});
//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