-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (40 loc) · 1.33 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
const express = require("express");
const path = require("path");
const mongoose = require("mongoose");
const cookieParser = require("cookie-parser");
const dotenv = require("dotenv").config();
const {
checkForAuthenticationCookie,
} = require("./middlewares/authentication");
const { validateToken } = require("./services/authentication");
const userRoute = require("./routes/user");
const postRouter = require("./routes/post");
const app = express();
const PORT = process.env.PORT;
mongoose
.connect(process.env.MONGODBURL)
.then(() => console.log("Connected to database"))
.catch((err) => console.log("Error Occurred:" + err));
app.set("view engine", "ejs");
app.set("views", path.resolve("./views"));
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(checkForAuthenticationCookie("token"));
app.use(express.static(path.resolve("./public")));
app.get("/", (req, res) => {
const token = req.cookies["token"];
if (token) {
const userPayload = validateToken(token);
req.user = userPayload;
return res.redirect("/user/profile");
}
res.render("home.ejs", {
sign: "Signup",
});
});
app.get("/about", (req, res) => {
return res.render("about.ejs");
});
app.use("/user", userRoute);
app.use("/post", postRouter);
app.listen(PORT, () => console.log(`Server started at Port: ${PORT}`));