-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
92 lines (76 loc) · 2.51 KB
/
server.js
File metadata and controls
92 lines (76 loc) · 2.51 KB
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
import "express-async-errors";
import * as dotenv from "dotenv";
dotenv.config();
import express from "express";
const app = express();
import morgan from "morgan";
import mongoose from "mongoose";
import cookieParser from "cookie-parser";
import helmet from "helmet";
import mongoSanitize from "express-mongo-sanitize";
// routers
import authRouter from "./routes/authRouter.js";
import userRouter from "./routes/userRouter.js";
import scheduleRouter from "./routes/scheduleRouter.js";
import levelRouter from "./routes/levelRouter.js";
import teacherRouter from "./routes/teacherRouter.js";
import subjectRouter from "./routes/subjectRouter.js";
import schoolRouter from "./routes/schoolRouter.js";
// public
import { dirname } from "path";
import { fileURLToPath } from "url";
import path from "path";
// middleware
import errorHandlerMiddleware from "./middleware/errorHandlerMiddleware.js";
import {
authenticateUser,
authorizePermissions,
} from "./middleware/authMiddleware.js";
// cloudinary.config({
// cloud_name: process.env.CLOUD_NAME,
// api_key: process.env.CLOUD_API_KEY,
// api_secret: process.env.CLOUD_API_SECRET,
// });
const __dirname = dirname(fileURLToPath(import.meta.url));
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
app.use(express.static(path.resolve(__dirname, "./client/dist")));
app.use(cookieParser());
app.use(express.json());
app.use(helmet());
app.use(mongoSanitize());
app.get("/", (req, res) => {
res.send("Hello World");
});
app.get("/api/v1/test", (req, res) => {
res.json({ msg: "test route" });
});
// app.use("/api/v1/jobs", authenticateUser, jobRouter);
app.use("/api/v1/users", authenticateUser, userRouter);
app.use("/api/v1/levels", authenticateUser, [
authorizePermissions("super-admin", "admin"),
levelRouter,
]);
app.use("/api/v1/subjects", authenticateUser, subjectRouter);
app.use("/api/v1/teachers", authenticateUser, teacherRouter);
app.use("/api/v1/schedules", authenticateUser, scheduleRouter);
app.use("/api/v1/schools", authenticateUser, schoolRouter);
app.use("/api/v1/auth", authRouter);
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "./client/dist", "index.html"));
});
app.use("*", (req, res) => {
res.status(404).json({ msg: "not found" });
});
app.use(errorHandlerMiddleware);
const port = process.env.PORT || 6666;
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);
}