-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
114 lines (89 loc) · 3.04 KB
/
server.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
const express = require("express");
const app = express();
const { engine } = require("express-handlebars");
const cookieSession = require("cookie-session");
const {
checkIfUserIsLoggedIn,
checkSigIdAvailable,
} = require("./middleware/auth");
const { hashPw, comparePw } = require("./middleware/pw-check");
const {
postSignature,
displayPetitionPage,
} = require("./middleware/petition_functions");
const {
getAllPetitionSigners,
getAllPetitionSignersByCity,
} = require("./middleware/signers_functions");
const {
getSigCountAndData,
deleteSig,
} = require("./middleware/thanks_functions");
const {
postProfileInformation,
getUserProfile,
updateUserProfiles,
getUserPage,
} = require("./middleware/profile_functions");
///////////////////////////////////////////// EXPRESS HANDLEBARS ////////////////////////////////////
app.engine("handlebars", engine());
app.set("view engine", "handlebars");
///////////////////////////////////////////// MIDDLEWARE /////////////////////////////////////////////
if (process.env.NODE_ENV == "production") {
app.use((req, res, next) => {
if (req.headers["x-forwarded-proto"].startsWith("https")) {
return next();
}
res.redirect(`https://${req.hostname}${req.url}`);
});
}
app.use(express.static("./public"))
.use(express.urlencoded({ extended: false }))
.use(
cookieSession({
secret:
process.env.COOKIE_SECRET ||
require("./secrets.json").COOKIE_SECRET,
maxAge: 1000 * 60 * 60 * 24 * 14,
sameSite: true,
})
)
.use((req, res, next) => {
res.setHeader("x-frame-options", "deny");
next();
});
///////////////////////////////////////////// ROUTES /////////////////////////////////////////////
app.get("/registration", checkIfUserIsLoggedIn, (req, res) =>
res.render("registration", { identifier: "registration" })
);
app.post("/registration", hashPw);
app.get("/login", checkIfUserIsLoggedIn, (req, res) =>
res.render("login", { identifier: "login" })
);
app.post("/login", comparePw);
app.get("/profile", getUserPage);
app.post("/profile", postProfileInformation);
app.get("/profile/edit", getUserProfile);
app.post("/profile/edit", updateUserProfiles);
app.get("/petition", displayPetitionPage);
app.post("/petition", postSignature);
app.get("/thanks", checkSigIdAvailable, getSigCountAndData);
app.post("/thanks/delete", deleteSig);
app.get("/signers", checkSigIdAvailable, getAllPetitionSigners);
app.get("/signers/:city", checkSigIdAvailable, getAllPetitionSignersByCity);
app.get("/logout", (req, res) => {
req.session.sigId = null;
req.session.userId = null;
res.redirect("/login");
});
app.get("*", checkIfUserIsLoggedIn, (req, res) =>
res.redirect("/registration")
);
///////////////////////////////////////////// FIRE UP SERVER
if (require.main == module) {
app.listen(process.env.PORT || 8080, () => {
console.log(`Petition server listening`);
});
}
// FOR SUPERTEST
module.exports.app = app;