-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
130 lines (110 loc) · 3.27 KB
/
main.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import express, { json } from 'express'
import path from 'path'
import { fileURLToPath } from 'url'
import webRoutes from './web/index.js'
import apiRoutes from './api/index.js'
import mongoose from 'mongoose'
import session from 'express-session'
import MongoStore from 'connect-mongo'
import passport from 'passport'
import 'dotenv/config'
import flash from 'connect-flash'
import cookieParser from 'cookie-parser'
import getUserById from './utils/getUSer.js'
import './config/passport.js'
const secret = process.env.SESSION_SECRET
const url = process.env.MONGODB_URI
const PORT = 3000
const app = express()
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*')
if (req.header('Access-Control-Request-Headers')) {
res.setHeader(
'Access-Control-Allow-Headers',
req.header('Access-Control-Request-Headers')
)
}
next()
})
//connect to db
mongoose
.connect(url)
.then(() => {
console.log('Connected to the Database successfully')
app.listen(PORT, () => {
console.log(
`Server running on port ${PORT}` + ' ' + `http://0.0.0.0:${PORT}/`
)
})
})
.catch((err) => {
console.log(err)
})
//view engine
app.set('view engine', 'ejs')
const __dirname = path.dirname(fileURLToPath(import.meta.url))
//static files
app.use(express.static(path.join(__dirname, 'src', 'sass')))
app.use(express.static(path.join(__dirname, 'src', 'assets', 'images')))
app.use(express.static(path.join(__dirname, 'src', 'assets', 'js')))
app.use(express.static(path.join(__dirname, 'src', 'assets', 'js', 'Teacher')))
app.use(express.static(path.join(__dirname, 'src', 'assets', 'svg')))
app.use(express.static(path.join(__dirname, 'src', 'css')))
app.use(express.static(path.join(__dirname, 'translations')))
//body parser
app.use(express.json({ limit: '50mb' }))
app.use(express.urlencoded({ limit: '50mb', extended: true }))
app.use(cookieParser())
app.use(flash())
//cors
//session config
app.use(
session({
secret: secret,
resave: false,
saveUninitialized: false,
store: new MongoStore({
client: mongoose.connection.getClient(),
}),
cookie: {
maxAge: 1000 * 60 * 60, // 1 hour
},
})
)
app.use(passport.initialize())
app.use(passport.session())
//routes
app.use('/', webRoutes)
app.use('/api', apiRoutes)
//ajax
app.get('/main/:file', async function (req, res) {
const filePath = path.join(__dirname, 'views/main', req.params.file)
const user = await getUserById(req.user?.roleData, req.user?.role)
res.render(filePath, { admin: user, loggedInUser: req.user })
})
//app.get('/main/:file', function (req, res) {
// res.sendFile(path.join(__dirname, 'views/main', req.params.file))
//})
app.get('/teacher/main/:file', async function (req, res) {
const filePath = path.join(__dirname, 'views/teacher/main', req.params.file)
const user = await getUserById(req.user?.roleData, req.user?.role)
res.render(filePath, {
loggedInTeacher: user,
teacher: req.user,
messages: 'merhaba',
})
})
//error handling
app.use((err, req, res, next) => {
res.json({
message: 'An error occurred',
url: req.url,
method: req.method,
errorStack: err.stack,
errorStatus: err.status,
errorStatusCode: err.statusCode,
errorMessage: err.message,
errorName: err.name,
})
})
// functions