-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·102 lines (73 loc) · 2.55 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
if (process.NODE_ENV != "production" ){
require("dotenv").config();
}
// requiring express and create an express application
const express = require('express')
const app = express();
//requiring connect-flash module
const flash = require('connect-flash');
// const dotenv=require('dotenv');
const path=require('path');
//requiring bcryptjs
const bcrypt=require("bcryptjs");
//requiring express-session
const sessions=require("express-session");
// dotenv.config();
const { db_handler } = require('./config/config');
const { router } = require("./Routes/index");
// Set up middlewars
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.set('view engine','ejs');
app.set('views', path.join(__dirname,'Views'));
app.use(express.static('public'));
// Importing mongo-store module
const mongostore = require("connect-mongo");
//Connecting to MySQL DB:
db_handler.connect((err) => {
if (err){
console.log("Ouups, cannot get connection to MySQL server! " + err.message);
}
else
console.log("Successfully connected to MySQL server");
});
/** SETUP OUR SESSIONS */
// console.log("mongo URL:",String(process.env.MONGO_ATLAS_SESSION_STORE_URL));
const sessionOption = {
name: String(process.env.SESSION_NAME),
secret: String(process.env.SESSION_SECRET),
resave: false,
saveUninitialized: false,
store: mongostore.create({
mongoUrl:String(process.env.MONGO_ATLAS_SESSION_STORE_URL),
touchAfter:5 * 24 * 60 * 60
}),
cookie: {
maxAge: 5 * 24 * 60 * 60 * 1000, // max_age = 5 days
httpOnly:true,
}
}
app.use(sessions(sessionOption));
app.use(flash());
// overide res.locals object in order to get flash messages
// middlware for flash messages in every http request
app.use((req, res, next) => {
res.locals.success = req.flash("success");
res.locals.danger = req.flash("danger");
res.locals.active_user_email = req.session.active_user_email;
res.locals.active_user_id = req.session.active_user_id;
res.locals.active_user_nickname=req.session.active_user_nickname;
// console.log("success >>",res.locals.success);
// console.log("danger >>",res.locals.danger);
// console.log("session >>>",req.session);
// console.log("user_email >>",res.locals.active_user_email);
// console.log("user_id >>",res.locals.active_user_id);
next()
})
//routes handler
app.use(router);
// launch application on port 3000
const port= process.env.PORT || 3000;
app.listen(port, () => {
console.log("app runs on [port:",port,"]");
})