forked from isaacssemugenyi/hackathon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
80 lines (64 loc) · 1.98 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
const express = require('express');
const mongoose = require('mongoose');
const session = require('express-session');
const passport = require('passport');
const path = require('path');
const app = express();
const config = require('./config/db');
const port = process.env.PORT || 3000;
app.locals.moment = require('moment');
// View engine
app.set('view engine', 'pug');
app.set('views', './views');
// Middlewares
app.use(express.json());
app.use(express.static(path.join(__dirname , 'public')));
// Importing routes
const defaultRoutes = require('./routes/defaultRouter')
const clientRoutes = require('./routes/clientRouter')
const depositRoutes = require('./routes/depositRouter')
// DB config
mongoose.connect(config.database, {
useNewUrlParser: true,
useUnifiedTopology: true
}
)
.then(once => {
console.log('Connected to DB')
app.listen(port, ()=> console.log(`Server running on port ${port}`))
})
.catch(err => console.log(err.message))
app.use(express.static(path.join(__dirname , 'public'))); //Using static files
app.use(express.urlencoded({extended: true})) // Accessing form data
// Express session middleware
app.use(session({
secret: 'mostsecretwordeverseen',
resave: true,
saveUninitialized: true
}));
// Express messages middleware
app.use(require('connect-flash')());
app.use(function (req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});
//Passport Config and middleware
require('./config/passport')(passport);
app.use(passport.initialize());
app.use(passport.session());
//Global variable for loggedin users
app.get('*', (req, res, next)=>{
res.locals.user = req.user || null;
next();
})
app.use('/', defaultRoutes)
app.use('/client', clientRoutes)
app.use('/deposit', depositRoutes)
//Handle missing routes
app.get('/500', (req, res)=>{
res.render('500', {page: 'Something went wrong'});
})
//Handle missing routes
app.get('*', (req, res)=>{
res.render('404', {page: 'Page not found'});
})