-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
96 lines (70 loc) · 2.38 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
// ℹ️ Gets access to environment variables/settings
// https://www.npmjs.com/package/dotenv
require("dotenv/config");
// ℹ️ Connects to the database
require("./db");
// Handles http requests (express is node js framework)
// https://www.npmjs.com/package/express
const express = require("express");
// Handles the handlebars
// https://www.npmjs.com/package/hbs
const hbs = require("hbs");
const app = express();
// ℹ️ This function is getting exported from the config folder. It runs most pieces of middleware
require("./config")(app);
const session = require("express-session");
const mongoStore = require('connect-mongo');
app.use(
session({
resave: true,
saveUninitialized: true,
secret: process.env.SESSION_SECRET,
cookie: {
sameSite: true, //frontend backend both run on localhost
httpOnly: true, //we are not using https
maxAge: 60000, //session time
},
rolling: true,
store: new mongoStore({
mongoUrl: process.env.MONGODB_URI,
ttl: 60 * 60 * 24 //1 day
})
})
);
function getCurrentLoggedUser(req, res, next) {
if (req.session && req.session.currentUser) {
app.locals.loggedInUser = req.session.currentUser.username;
} else {
app.locals.loggedInUser = "";
}
next();
}
app.use(getCurrentLoggedUser);
// default value for title local
const projectName = "populis";
const capitalized = (string) => string[0].toUpperCase() + string.slice(1).toLowerCase();
app.locals.title = `${capitalized(projectName)}`;
// 👇 Start handling routes here
const index = require("./routes/index");
app.use("/", index);
const index2 = require("./routes/index");
app.use("/", index2);
const deputies = require("./routes/deputy");
app.use("/", deputies);
const voting = require("./routes/voting");
app.use("/", voting);
const news = require("./routes/news");
app.use("/", news);
const user = require("./routes/user");
app.use("/", user);
const parties = require("./routes/parliament");
app.use("/", parties);
const auth = require("./routes/auth");
app.use("/", auth);
const poll = require('./routes/voting');
app.use("/", poll);
const info = require ('./routes/info');
app.use("/", info);
// ❗ To handle errors. Routes that don't exist or errors that you handle in specific routes
require("./error-handling")(app);
module.exports = app;