forked from chen-robert/stratus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (54 loc) · 1.73 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require("dotenv").config();
var express = require("express");
var path = require("path");
const db = require(__dirname + "/server/db");
const cookieSession = require("cookie-session");
const PORT = process.env.PORT || 3000;
const app = express();
// Sessions
app.use(cookieSession({
name: "session",
secret: process.env.SECRET || "cas-project ihs",
resave: false,
saveUninitialized: true,
maxAge: 100 * 365 * 24 * 60 * 60 * 1000
}));
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: false }));
app.use(require("less-middleware")(__dirname + "/public"));
app.use(express.static(path.join(__dirname, "public")));
const requireAuth = (req, res, next) => {
if (req.session.uid) {
next();
} else {
req.session.error = "Login required";
res.redirect("/login");
}
};
// https://stackoverflow.com/questions/5251520/how-do-i-escape-some-html-in-javascript/5251551
function escapeHTML (s) {
return s.replace(/&/g, "&")
.replace(/"/g, """)
.replace(/</g, "<")
.replace(/>/g, ">");
}
app.use(function (req, res, next) {
var err = req.session.error;
var msg = req.session.success;
delete req.session.error;
delete req.session.success;
res.locals.message = "";
if (err) res.locals.message = '<p class="msg error">' + escapeHTML(err) + "</p>";
if (msg) res.locals.message = '<p class="msg success">' + escapeHTML(msg) + "</p>";
next();
});
app.use("/login", require("./server/routes/login"));
app.get("/logout", (req, res) => {
req.session = null;
res.redirect("/");
});
app.use(requireAuth);
app.use("/", require("./server/routes/index"));
app.listen(PORT, () => console.log(`Listening on port: ${PORT}`));