-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (50 loc) · 1.5 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
const express = require('express');
const session = require('express-session');
const passport = require('./middleware/passport');
const flash = require('connect-flash');
const expressLayouts = require('express-ejs-layouts');
const methodOverride = require('method-override');
// const { errorHandler, notFoundHandler } = require('./middleware/error')
require('dotenv').config();
const app = express();
const port = process.env.PORT || 3000
//db config
const db = require('./dbconfig')
db.connection();
// set up ejs engine
app.use(expressLayouts);
app.set('view engine', 'ejs');
// set up middelware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(methodOverride('_method'));
//Express session
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
}));
//passport middleware
app.use(passport.initialize());
app.use(passport.session());
// connect flash
app.use(flash())
// Global variables for flash
app.use((req, res, next) => {
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.errors = req.flash('error');
next();
});
// set error middleware
// app.use(notFoundHandler);
// app.use(errorHandler);
// Routes
app.use('/', require('./routes/index'));
app.use('/auths', require('./routes/auth'));
app.use('/quizzes', require('./routes/quiz'));
// start server
app.listen(port, () => {
console.log(`Server started on port ${port}...`)
});
module.exports = app;