This repository has been archived by the owner on Jan 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (58 loc) · 1.91 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
require("dotenv").config();
// import express
const express = require("express");
const app = express();
const port = 3001;
// import error handling middleware
const errorHandling = require("./apiFunctions/errorHandling");
// import session management
var session = require("express-session");
var MySQLStore = require("express-mysql-session")(session);
// import routing
var user = require("./src/user");
var course = require("./src/course");
// import mysql credentials
// for compremizing the web request
var compression = require("compression");
app.use(compression());
// security package
var helmet = require("helmet");
app.use(helmet());
// define the store of our sessions (it is our database)
var sessionStore = new MySQLStore({
host: process.env.DB_HOST,
user: process.env.DB_USER,
port: process.env.DB_PORT,
password: process.env.DB_PASS,
database: process.env.DB_DATA
});
// make the app use the session management
app.use(
session({
// sessionID secret, will be changes in production
secret: "keyboard cat",
/* set the store as our previously defined databse store, otherwise the session
will be stored in the process */
store: sessionStore,
/* this defines th result of unsetting req.session,
'destroy' will delete the session */
unset: "destroy",
// this disables the save to the store if nothing changed to the session
resave: false,
// this will not save unchanged sessions, it releases some database storage
saveUninitialized: false,
cookie: {
// define, when the coockie expires
maxAge: 10800000 /*3 hours*/,
// lax enables the coockie everywhere on our site(correct me if I'm wrong)
sameSite: "lax"
}
})
);
// setup router
app.use("/api/user", user);
app.use("/api/course", course);
// errorHandling Middleware
app.use(errorHandling);
// start the app
app.listen(port, () => console.log(`Example app listening on port ${port}!`));