-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
87 lines (67 loc) · 2.44 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
// Importing all necessary packages
import express from "express";
import mongoose from "mongoose";
import * as dotenv from "dotenv";
import cors from 'cors';
import cookieParser from "cookie-parser";
import morgan from 'morgan';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import path from 'path';
dotenv.config();
// Import routers and middlewares
import authRouter from './routes/authRouter.js';
import userRouter from './routes/userRouter.js';
import adminRouter from './routes/adminRouter.js';
import staffRouter from './routes/staffRouter.js';
import ratingRouter from './routes/ratingRouter.js';
import feedbackRouter from './routes/feedbackRouter.js';
import departRatingRouter from './routes/departRatingRouter.js';
import { getUserRole } from "./controllers/userController.js";
import errorHandlerMiddleware from "./middleware/ErrorHandleMiddleware.js";
import { errorHandler } from "./middleware/ErrorMiddleware.js";
// Default export
const app = express();
const port = 3333;
// Use express.json() instead of bodyParser.json()
app.use(express.json());
app.use(cookieParser());
app.use(cors());
app.use(morgan('dev'));
app.use(errorHandler);
// app.use(errorHandlerMiddleware);
const __dirname = dirname(fileURLToPath(import.meta.url));
// Serve static files
app.use(express.static(path.resolve(__dirname, './public')));
// Routes
app.get('/', (req, res) => {
res.send("hello world");
});
app.use("/api/v1/auth", authRouter);
app.use("/api/v1/dashboard-student/current-user", userRouter);
app.use("/api/v1/depart-ratings", departRatingRouter);
app.use("/api/v1/dashboard-admin", adminRouter);
app.use("/api/v1/dashboard-student/feedbacks", feedbackRouter);
app.use("/api/v1/dashboard-head/staff", staffRouter);
app.use("/api/v1/users/dashboard-admin/:email", getUserRole);
app.use("/api/v1/dashboard-head/:id", staffRouter);
app.use("/api/v1/dashboard-student/ratings", ratingRouter);
// Handle all other routes with React's index.html
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, './public', 'index.html'));
});
// Error handling routes
app.use("*", (req, res) => {
res.status(404).json({ msg: "not found" });
});
app.use(errorHandlerMiddleware);
// Listen on port and connect to MongoDB
try {
await mongoose.connect(process.env.MONGO_URL);
app.listen(port, () => {
console.log(`server running on PORT ${port}....`);
});
} catch (error) {
console.log(error);
process.exit(1);
}