-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
86 lines (69 loc) · 2.67 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
const express = require('express');
const mongoose = require('mongoose');
const Student = require('./models/student.model');
const Teacher = require('./models/teacher.model');
const teachersRoute = require('./routes/teachers.route');
const studentsRoute = require('./routes/students.route');
const homepageRoute = require('./routes/homepage.route');
const adminRoute = require('./routes/admin.route');
const attendanceRecordsRoute = require('./routes/attendanceRecords.route');
const session = require('express-session');
const path = require('path');
const cors = require('cors');
const app = express();
require('dotenv').config();
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname, 'index-file')));
app.use(express.static(path.join(__dirname, 'teachers-login')));
app.use(express.static(path.join(__dirname, 'student-portal')));
app.use(express.static(path.join(__dirname, 'admin-login')));
app.use('/student-portal/student-dashboard', express.static(path.join(__dirname, 'student-portal/student-dashboard')));
app.use('/teachers-login/teachers-dashboard', express.static(path.join(__dirname, 'teachers-login/teachers-dashboard')));
app.use('/admin-login/admin-dashboard', express.static(path.join(__dirname, 'admin-login/admin-dashboard')));
app.use('/404-resources', express.static(path.join(__dirname, '404page', '404-resources')));
app.use(express.static(path.join(__dirname, '404page')));
// Session middleware
app.use(session({
secret: 'cors-key-value',
resave: false,
saveUninitialized: true,
cookie: { secure: false, httpOnly: true }
}));
// Routing
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index-file', 'index.html'));
});
//health
app.get('/health', (req, res) => {
res.status(200).send('OK');
});
// Route-servings
app.use('/teachers', teachersRoute);
app.use('/students', studentsRoute);
app.use('/attendanceRecords', attendanceRecordsRoute); // Fixed typo
app.use('/homepage', homepageRoute);
app.use('/adminaccess', adminRoute);
//404 Error Handling
app.use((req, res, next) => {
res.status(404).sendFile(path.join(__dirname, '404page', '404.html'));
});
// Database and port connections
const PORT = process.env.PORT || 4000;
const MONGODB_URI = process.env.MONGODB_URI;
mongoose.connect(MONGODB_URI)
.then(() => {
console.log("Connected to the database");
app.listen(PORT, () => {
console.log(`The app is listening at http://localhost:${PORT}/`);
});
})
.catch((error) => {
console.error("Connection to the database failed!", error);
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something broke!');
});