-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
64 lines (53 loc) · 1.41 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
const express = require("express");
const app = express();
const session = require("express-session");
app.use(
session({
name: "AuthCookie",
secret: "4kAQJRzpPSJ27pttoqejyh8RsjfMFEJXeGqBCL5p4ow4HkszhbXjux8kWWr9BYpC",
saveUninitialized: true,
resave: false,
cookie: { maxAge: 600000 },
})
);
app.use(function (req, res, next) {
res.locals.session = req.session;
next();
});
const configRoutes = require("./routes");
const static = express.static(__dirname + "/public");
const exphbs = require("express-handlebars");
app.use("/public", static);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.engine(
"handlebars",
exphbs.engine({
defaultLayout: "main",
})
);
app.set("view engine", "handlebars");
var hbs = exphbs.create({});
hbs.handlebars.registerHelper("dateFormat", require("handlebars-dateformat"));
hbs.handlebars.registerHelper("if_eq", function (a, b, opts) {
if (a == b) {
return opts.fn(this);
} else {
return opts.inverse(this);
}
});
hbs.handlebars.registerHelper("json", function (context) {
return JSON.stringify(context);
});
// middleware to check if user is admin for /manager route
app.use("/manager", (req, res, next) => {
if (req.session.isAdmin) {
next();
} else {
res.redirect("/home");
}
});
configRoutes(app);
app.listen(3000, () => {
console.log("Your routes will be running on http://localhost:3000");
});