-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
83 lines (73 loc) · 1.97 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
const express = require('express');
require('dotenv').config();
const mongoose = require('mongoose');
const session = require('express-session');
const MongoStore = require('connect-mongo');
const flash = require('connect-flash');
const methodOverride = require('method-override');
const pageRoute = require('./routes/pageRoute');
const courseRoute = require('./routes/courseRoute');
const categoryRoute = require('./routes/categoryRoute');
const userRoute = require('./routes/userRoute');
const ejs = require('ejs');
const app = express();
//TEMPLATE ENGINE
app.set('view engine', 'ejs');
//GLOBAL VARIABLES
global.userIN = null;
//MIDDLEWARES
app.use(express.static(__dirname + '/public'));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(
session({
secret: process.env.SECRET,
resave: false,
saveUninitialized: true,
store: MongoStore.create({ mongoUrl: process.env.MONGO_URI }),
})
);
app.use(flash());
app.use((req, res, next) => {
res.locals.flashMessages = req.flash();
next();
})
app.use(
methodOverride('_method', {
methods: ['POST', 'GET'],
})
);
// Logger
app.use((req, res, next) => {
userIN = req.session.userID;
console.table({
request: {
METHOD: req.method,
URL: req.url,
IP: req.ip,
USER_ID: req.session.userID,
SESSION_ID: req.sessionID,
ROLE: req.session.userRole,
},
});
next();
});
//ROUTES
app.use('/', pageRoute);
app.use('/courses', courseRoute);
app.use('/categories', categoryRoute);
app.use('/users', userRoute);
mongoose
.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log('DB Connected!');
app.listen(process.env.PORT, () => {
console.log(`Server is live on port ${process.env.PORT}`);
});
})
.catch((err) => {
console.log(err);
});