-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.js
63 lines (49 loc) · 1.71 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
require("dotenv").config();
const express = require("express");
const app = express();
const { logger, logEvents } = require("./middleware/logger");
const errorHandler = require("./middleware/errorHandler");
const cookieParser = require("cookie-parser");
const cors = require("cors");
const corsOptions = require("./config/corsOptions");
const connectDB = require("./config/dbConn");
const mongoose = require("mongoose");
const PORT = process.env.PORT || 3500;
connectDB();
app.use(logger);
app.use(cors(corsOptions));
app.use(express.json());
app.use(cookieParser());
app.use("/", express.static("public"));
app.use("/", require("./routes/root"));
app.use("/auth", require("./routes/authRoutes"));
app.use("/paper", require("./routes/paperRoutes"));
app.use("/notes", require("./routes/notesRoutes"));
app.use("/internal", require("./routes/internalRoutes"));
app.use("/attendance", require("./routes/attendanceRoutes"));
app.use("/time_schedule", require("./routes/timeScheduleRoutes"));
app.use("/staff", require("./routes/staffRoutes"));
app.use("/student", require("./routes/studentRoutes"));
app.all("*", (req, res) => {
res.status(404);
if (req.accepts("json")) {
res.json({ message: "404 Not Found", details: "No paths found" });
} else {
res.type("txt").send("404 Not Found");
}
});
app.use(errorHandler);
mongoose.connection.once("open", () => {
console.log("Connected to MongoDB");
app.listen(PORT, () => console.log(`server running on PORT ${PORT}`));
});
mongoose.connection.on("error", (err) => {
console.log(err);
logEvents(
`${err.no}:${err.code}\t${err.syscall}\t${err.hostname}`,
"mongoErrLog.log"
);
});
mongoose.connection.on("uncaughtException", function (err) {
console.log(err);
});