-
Notifications
You must be signed in to change notification settings - Fork 40
/
app.js
86 lines (68 loc) · 2.45 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
/* eslint-disable no-console */
const express = require('express');
const mongoose = require('mongoose');
const exphbs = require('express-handlebars');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const flash = require('connect-flash');
const session = require('express-session');
// const expressOasGenerator = require('express-oas-generator');
const path = require('path');
const passport = require("passport");
// const RedisStore = require('connect-redis')(session);
var expressValidator = require('express-validator');
const database = require('./config/key').MongoURI;
const indexRoute = require('./routes/index');
// Set up the app with express
const app = express();
// set up views path
app.set('views', path.join(__dirname, 'views'));
app.engine('.hbs', exphbs({ defaultLayout: 'main', extname: '.hbs' }));
// view engine
app.set('view engine', 'hbs');
// set up morgan
app.use(logger('dev'));
// middlewares
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// connect flash
app.use(flash());
app.use(session({ secret: 'dragonbeast4theTrophy', saveUninitialized: true, resave: true }));
// generate api docs (Swagger)
// expressOasGenerator.init(app, function(spec) {
// _.set(spec, 'info.title', 'New Title');
// _.set(spec, "paths['/path'].get.parameters[0].example", 2);
// return spec;
// });
app.use(passport.initialize());
app.use(passport.session());
require('./config/passport')(passport);
// connect to database
// eslint-disable-next-line no-unused-vars
mongoose.connect(database, { useNewUrlParser: true }, (err, client) => {
if (err) console.log(err);
console.log('Mongodb Connection passed');
});
const db = mongoose.connection;
db.once('open', () => console.log('Connected to Mongodb'));
// check if error
db.on('error', console.error.bind(console, 'Mongodb connection error:'));
// API ROUTES
app.use('/', indexRoute);
app.use(expressValidator());
// Express will serve up 404 file if a route is not recognized
app.get('*', (req, res) => {
res.render(path.resolve(__dirname, 'views', '404'));
});
// error handler
app.use((err, req, res) => {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;