-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
136 lines (114 loc) · 3.42 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const express = require('express');
const path = require('path');
const exphbs = require('express-handlebars');
const session = require('express-session');
const bodyParser = require('body-parser');
const passport = require('passport');
const flash = require('connect-flash');
const mime = require('./config/mimeConfig');
const mySqlStore = require('express-mysql-session');
const db = require('./config/db');
const auth = require('./config/passport');
const hbs = require('./helpers/hbs');
const outsourceDb = require('./config/dbConnection');
outsourceDb.setUpDB(false);
const notifications = require('./models/notifications');
//PayPal
var paypal = require('paypal-rest-sdk');
paypal.configure({
mode: 'sandbox', // Sandbox or live
client_id: 'AU0gOYOjaL87P0T2vYWhfI8H62bKOvPCBxn3fI8kM9VqAueU9-O_0xYWbNVaHXo2FgI6Nmwlb0OPhrAJ',
client_secret: 'ECG4YL9F_2UWuT5yGSLKO90SfcMfx6aVb7xRzFX3KD42hNj0yenw8BinuTQ6XK3v8sjutoeFzC9LmOZM'
});
const app = express();
// Static Folder
app.use(express.static(path.join(__dirname, 'public/uploads'), { maxAge: 0 }));
app.use(express.static(path.join(__dirname, 'public')));
// Session
app.use(session({
secret: '$2y$10$1fs/4go1gKR39/cXficd0eG1qg16/Fj.UxNI4WQelTbWzXOzp8tBS',
store: new mySqlStore({
host: db.host,
port: 3306,
user: db.username,
password: db.password,
database: db.database,
clearExpired: true,
checkExpirationInterval: 43200000, // 12 Hours
expiration: 86400000
}),
resave: false,
saveUninitialized: false,
}));
// Passport
app.use(passport.initialize());
app.use(passport.session());
auth.localStrategy(passport);
// Handlebars
app.engine('handlebars', exphbs({
helpers: hbs,
defaultLayout: 'base',
layoutsDir: __dirname + '/views/layouts',
partialsDir: hbs.partialsDirs(__dirname + '/views/partials')
}));
app.set('view engine', 'handlebars');
// Connect Flash
app.use(flash());
// Global Variable
app.use(function (req, res, next) {
res.locals.user = req.user || null;
res.locals.url = req.originalUrl;
res.locals.forms = req.flash('forms');
if (Object.getOwnPropertyNames(res.locals.forms).length < 1) {
delete res.locals.forms;
}
else {
res.locals.forms = res.locals.forms[0];
}
toast = {
'info': req.flash('info'),
'warning': req.flash('warning'),
'success': req.flash('success'),
'error': req.flash('error'),
}
Object.keys(toast).forEach(key => {
if (toast[key].length < 1) {
delete toast[key];
}
});
res.locals.toast = Object.keys(toast).length > 0 ? toast : null;
// Notifications
if (res.locals.user != null) {
notifications.findAll({
where: { user: res.locals.user.id }
}).then(notifications => {
res.locals.notifNum = notifications.length
next();
});
}
else{
next();
}
});
// Body Parser
app.use(bodyParser.urlencoded({
limit: '100mb',
extended: true,
parameterLimit: 50000
}));
// Init mime custom mapping
mime.init();
// Routes
app.use('/', require('./routes/root'));
app.use('/', require('./routes/auth'));
app.use('/profile', require('./routes/profile'));
app.use('/services', require('./routes/services'));
app.use('/files', require('./routes/files'));
app.use('/jobs', require('./routes/jobs'));
app.use(function (req, res) {
req.flash('error', 'Page not found.');
res.redirect('/');
});
app.listen(port = 5000, () => {
console.log(`\n\x1b[32mServer started on port ${port}.\x1b[0m`);
});