-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
142 lines (125 loc) · 3.16 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
const express = require("express");
const mongoose = require("mongoose");
const ejs = require("ejs");
const path = require("path");
const Students = require("./models/student");
const Admin = require("./models/admin");
const studentRouter = require("./routes/student");
const adminRouter = require("./routes/admin");
const bodyParser = require("body-parser");
const bcrypt = require("bcrypt");
const flash = require("connect-flash");
const sessions = require("express-session");
const methodOverride = require("method-override");
const app = express();
const PORT = 8000;
const oneDay = 1000 * 60 * 60 * 24;
mongoose.set("strictQuery", "false");
mongoose
.connect(process.env.MONGODB_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("Connected to DB");
})
.catch((error) => {
console.log(error);
});
app.set("view engine", "ejs");
app.use(
sessions({
name: `userSession`,
secret: "some-secret-example",
resave: false,
saveUninitialized: false,
cookie: {
maxAge: oneDay,
},
})
);
app.use(express.static(path.join(__dirname, "/public")));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(flash());
app.use(methodOverride("_method"));
app.get("/", async (req, res) => {
let message = req.flash("message");
let allStudent = await Students.find({});
let hidden = "hidden";
console.log(req.session.email);
if (req.session.email) {
res.render("index", {
message: message,
Students: allStudent,
specialOption: "",
option: "Logout",
});
} else {
res.render("index", {
message: message,
Students: allStudent,
specialOption: hidden,
option: "Login",
});
}
});
app.get("/registration", (req, res) => {
let message = req.flash("message");
let alert = req.flash("alert");
if (req.session.email) {
res.render("registration", {
message: message,
specialOption: "",
option: "Logout",
alertMessage: alert,
});
} else {
res.send("Oops something went wrong");
}
});
app.get("/submission", (req, res) => {
let message = req.flash("message");
let alert = req.flash("alert");
if (req.session.email) {
res.render("submission", {
message: message,
specialOption: "",
option: "Logout",
alertMessage: alert,
});
} else {
res.send("Oops something went wrong");
}
});
app.get("/login", (req, res) => {
let message = req.flash("message");
let alert = req.flash("alert");
if (req.session.email) {
res.render("login", {
message: message,
specialOption: "",
option: "Logout",
alertMessage: alert,
});
} else {
res.render("login", {
message: message,
specialOption: "hidden",
option: "Login",
alertMessage: alert,
});
}
});
app.get("/logout", (req, res) => {
req.session.destroy();
res.redirect("/");
});
app.post("/subscribe", (req, res) => {
req.flash("message", "Subscribed MD Manager, will get the updates");
res.redirect("/");
});
app.use("/student", studentRouter);
app.use("/admin", adminRouter);
app.listen(PORT, () => {
console.log(`listening on http://localhost:${PORT}`);
});