-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
81 lines (66 loc) · 2.45 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
//imports built-in
const path = require("path");
//imports 3rd party
const express = require("express");
const expressSession = require("express-session");
const csrf = require("csurf");
//imports custom
const db = require("./data/database");
const createSessionConfig = require("./config/session-config");
const addCsrfTokenMiddleware = require("./middlewares/csrf-token-middleware");
const notFoundMidlleware = require("./middlewares/not-found-middleware");
const errorHandlingMiddleware = require("./middlewares/error-handling-middleware");
const baseRoutes = require("./routes/base-routes");
const gameConfigRoutes = require("./routes/game-config-routes");
const gameRoomRoutes = require("./routes/game-room-routes");
const gamePlayRoutes = require("./routes/game-play-routes");
const playerRoutes = require("./routes/player-routes");
const Room = require("./models/room-model");
//create express app
const app = express();
//configure EJS template engine for this app
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
//static files midldeware
app.use(express.static("public"));
//urlencoded body parser
app.use(express.urlencoded({ extended: false }));
//JSON body parser
app.use(express.json());
//express-session middleware
const sessionConfig = createSessionConfig();
app.use(expressSession(sessionConfig));
//csrf protection
app.use(csrf());
//generate CSRF token
app.use(addCsrfTokenMiddleware);
//routes registration
app.use(baseRoutes);
app.use("/game", gameConfigRoutes);
app.use("/game", gameRoomRoutes);
app.use("/game", gamePlayRoutes);
app.use("/player", playerRoutes);
//not found middleware
app.use(notFoundMidlleware);
//error handling middleware
app.use(errorHandlingMiddleware);
//init the correct port number using an environment variable ! !
let portNumber = 3000;
if (process.env.PORT) {
portNumber = process.env.PORT;
}
//start server only after connection to database has established without errors
db.connectToDatabase()
.then(function () {
//start web server
app.listen(portNumber);
//start cyclical process for cleaning inactive rooms in the DB
//NOTE: every 3 hour it deletes the rooms which have been inactive for more than 3 hours
const oneHour_ms = 1000 * 60 * 60;
Room.deleteInactiveRoomsCiclically(1 * oneHour_ms, 1 * oneHour_ms);
})
.catch(function (error) {
//catch error if connection to database fails
console.log("Failed to connect to the database");
console.log(error);
});