This repository has been archived by the owner on Dec 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
82 lines (71 loc) · 2.15 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
const express = require('express');
const passport = require('passport');
const ejsLayouts = require('express-ejs-layouts');
const session = require('express-session');
const path = require('path');
const app = express();
const uuid = require('uuid');
const helmet = require('helmet');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const MongoStore = require('connect-mongo')(session);
const { Views, Discord, Messages } = require('./routers');
require('dotenv-flow').config();
const dbOptions = {
useNewUrlParser: true,
autoIndex: false,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 500,
poolSize: 5,
connectTimeoutMS: 10000,
family: 4
};
app.use(helmet());
mongoose.connect(process.env.MONGO_URI, dbOptions);
mongoose.Promise = global.Promise;
const db = mongoose.connection;
app.use(session({
genid: (req) => {
return uuid();
},
name: process.env.SNAME,
secret: process.env.SSECRET,
resave: false,
saveUninitialized: true,
store: new MongoStore({ mongooseConnection: db })
}));
app.use(passport.initialize());
app.use(passport.session());
app.set('view engine', 'ejs');
app.use(ejsLayouts);
app.set('layout', 'includes/main');;
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/public', express.static(path.join(__dirname, '/public')));
app.set('views', path.join(__dirname, '/views'));
app.use('/', Views);
app.use('/', Discord);
app.use('/', Messages);
app.use((err, req, res, next) => {
switch (err.message) {
case 'NoCodeProvided':
return res.status(400).send({
status: 'ERROR',
error: err.stack
});
default:
return res.status(500).send({
status: 'ERROR',
error: err.stack
});
}
});
app.listen(process.env.PORT, (err) => {
if (err) return console.log(err);
console.info(`Success! Listening at ${process.env.PORT}`);
});
process.on('unhandledRejection', error => {
console.log(`Unhandled Rejection: ${error}`);
});