-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
74 lines (63 loc) · 2.14 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
const express = require('express');
require('dotenv').config();
const mongo = require('mongodb');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const methodOverride = require('method-override')
const flash = require('connect-flash');
const session = require('express-session');
const passport = require('passport');
const cookieParser = require('cookie-parser');
const MongoStore = require('connect-mongo');
//Passport config
require('./config/passport')(passport);
//express app
const app = express();
//connect to mongodb
const uri = process.env['MONGO_URI']
mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true })
.then((result) => app.listen(process.env.PORT || 3000))
.catch((err) => console.log(err));
mongoose.set('useFindAndModify', false);
//register view engine
app.set('view engine', 'ejs');
//middleware and static files
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true })); //allows you to use req.body
app.use(methodOverride('_method')); //allows you to use PUT with a form
app.use(cookieParser());
//Express Session Middleware
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true,
store: MongoStore.create({
mongoUrl: process.env['MONGO_URI'],
mongoOptions: { useUnifiedTopology: true },
ttl: 2 * 24 * 60 * 60
})
}));
//Passport Middleware
app.use(passport.initialize());
app.use(passport.session());
//Connect flash
app.use(flash());
//Global Vars
app.use((req, res, next) => {
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.warning_msg = req.flash('warning_msg');
res.locals.error = req.flash('error');
res.locals.info = req.flash('info');
res.locals.dashboard_success_msg = req.flash('dashboard_success_msg');
res.locals.dashboard_info_msg = req.flash('dashboard_info_msg');
res.locals.dashboard_error_msg = req.flash('dashboard_error_msg');
next();
});
// Routes
app.use('/', require('./routes/index'))
app.use('/users', require('./routes/users'))
// 404 - must be at bottom
app.use((req, res) => {
res.status(404).render('404', { title: '404'})
})