-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
81 lines (50 loc) · 1.49 KB
/
server.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
/** IMPORT OUR DEPENDENCIES */
const express = require("express");
const app = express();
const path = require('path');
const sessions = require("express-session");
const flash = require("connect-flash");
const dotenv = require("dotenv");
/** IMPORT OUR ENVS */
dotenv.config()
/** CONNECT TO OUR MYSQL INSTENCE */
const { db_handler } = require("./database/config/mysql.conf");
(() => {
db_handler.connect((err) => {
if (err) {
console.log("Error payload is set to: %s", err.message);
return;
}
console.log("Database connected.");
});
})();
/** SETUP OUR EXPRESS APP SETTINGS */
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
/** SETUP OUR SESSIONS */
const sessionOption = {
name: String(process.env.SESSION_NAME),
secret: String(process.env.SESSION_SECRET),
resave: false,
saveUninitialized: false
}
app.use(sessions(sessionOption));
/** LAUNCH THE FLASH MIDDLEWARE */
app.use(flash());
/** SETUP SOME CUSTOM MIDDLEWARES */
app.use((req, res, next) => {
res.locals.flashMessages = {
success: req.flash("success"),
error: req.flash("error")
}
console.log('flashMessages :>> ', res.locals.flashMessages);
console.log('res.locals :>> ', res.locals);
next();
})
/** IMPORT & USE OUR ROUTES */
const router = require("./routes/index");
app.use(router);
/** LAUNCH OUR SERVER */
app.listen(3000, () => {
console.log("Server launched on port : %d", 3000);
})