-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
120 lines (102 loc) · 2.89 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
const express = require("express");
require("express-async-errors");
const app = express();
app.set("view engine", "ejs");
app.use(require("body-parser").urlencoded({ extended: true }));
require("dotenv").config(); // to load the .env file into the process.env object
const session = require("express-session");
// uses local storage to store session cookies data, replace with storing cookie in database
/*
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
})
);
*/
// uses mongodb to store session cookies data
const MongoDBStore = require("connect-mongodb-session")(session);
const url = process.env.MONGO_URI;
// error check to make sure the MONGO_URI is defined
if (!url) {
console.error("MONGO_URI is not defined in the environment variables.");
process.exit(1);
}
const store = new MongoDBStore({
// may throw an error, which won't be caught
uri: url,
collection: "mySessions",
});
store.on("error", function (error) {
console.log("MongoDBStore error: ", error);
});
const sessionParms = {
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
store: store,
cookie: { secure: false, sameSite: "strict" },
};
if (app.get("env") === "production") {
app.set("trust proxy", 1); // trust first proxy
sessionParms.cookie.secure = true; // serve secure cookies
}
app.use(session(sessionParms));
app.use(require("connect-flash")());
// secret word handling
// let secretWord = "syzygy";
app.get("/secretWord", (req, res) => {
if (!req.session.secretWord) {
req.session.secretWord = "syzygy";
}
res.locals.info = req.flash("info");
res.locals.errors = req.flash("error");
res.render("secretWord", { secretWord: req.session.secretWord });
});
app.post("/secretWord", (req, res) => {
if (req.body.secretWord.toUpperCase()[0] == "P") {
req.flash("error", "That word won't work!");
req.flash("error", "You can't use words that start with p.");
} else {
req.session.secretWord = req.body.secretWord;
req.flash("info", "The secret word was changed.");
}
res.redirect("/secretWord");
});
/*
app.post("/secretWord", (req, res) => {
req.session.secretWord = req.body.secretWord;
res.redirect("/secretWord");
});
*/
// secret word handling
// old way
/*
let secretWord = "syzygy";
app.get("/secretWord", (req, res) => {
res.render("secretWord", { secretWord });
});
app.post("/secretWord", (req, res) => {
secretWord = req.body.secretWord;
res.redirect("/secretWord");
});
*/
app.use((req, res) => {
res.status(404).send(`That page (${req.url}) was not found.`);
});
app.use((err, req, res, next) => {
res.status(500).send(err.message);
console.log(err);
});
const port = process.env.PORT || 3000;
const start = async () => {
try {
app.listen(port, () =>
console.log(`Server is listening on port ${port}...`)
);
} catch (error) {
console.log(error);
}
};
start();