This repository has been archived by the owner on Jan 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
87 lines (74 loc) · 2.37 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
81
82
83
84
85
86
87
require('dotenv').config()
//modules
const express = require('express'),
session = require('cookie-session'),
passport = require('passport'),
passportInit = require('./utils/passportConfig.js'),
flash = require('express-flash'),
expressLayouts = require('express-ejs-layouts'),
mongoose = require('mongoose'),
nodemailer = require('nodemailer'),
path=require('path'),
request = require('request');
//routes
const loginRouter = require('./routers/loginRouter.js'),
dashboardRouter = require('./routers/dashboardRouter.js'),
adminRouter = require('./routers/adminRouter.js'),
{ discoIt } = require('./utils/discordBot'),
userSchema = require('./schemas/userSchema.js'),
teamSchema = require('./schemas/teamSchema.js');
const { User } = require('discord.js');
const app = express(),
PORT = process.env.PORT || 5000;
//app middleware
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static('public'))
app.use(express.json({ limit: '1mb' }), express.urlencoded({ extended: true, limit: '1mb' }))
app.use(flash())
app.use(expressLayouts)
app.use('/', express.static('public'))
//passport middleware
app.use(session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
}))
passportInit(passport)
//connect mongodb
const dbUri = process.env.MONGO_URI
mongoose.connect(dbUri, { useNewUrlParser: true, useUnifiedTopology: true }).then(console.log("Connected to mongodb"))
// //discord
// botInit()
//more passport
app.use(passport.initialize())
app.use(passport.session())
function checkadmin(req, res, next) {
if (req.user.admin) {
return next()
}
res.redirect('/dashboard')
}
//main
app.get('/', (req, res) => {
res.render('index', { user: req.user })
});
app.use('/login', loginRouter)
app.use('/admin', checkadmin, adminRouter)
app.use('/dashboard', dashboardRouter)
app.get('/logout', (req, res) => {
req.logout();
res.redirect('/login');
});
// app.use((err, req, res, next) => {
// discoIt(err.stack.toString())
// discoIt("App Has Crashed, Please Check The Logs, Trying To Restart On My Own!");
// next()
// })
app.get('/test', async(req, res) => {
res.send(await userSchema.find({regType:"school"}))
})
app.listen(PORT, () => {
console.log(`Server started on port ${PORT}`)
discoIt("Server started on port " + PORT)
});