-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (48 loc) · 1.79 KB
/
index.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
const fs = require("fs");
const path = require("path");
const fileUpload = require("express-fileupload");
const PORT = process.env.PORT || 5000;
const express = require("express");
const bodyParser = require("body-parser");
const StudentRoutes = require("./routes/StudentRoutes");
const TeacherRoutes = require("./routes/TeacherRoutes");
const AdminRoutes = require("./routes/AdminRoutes");
const SharedRoutes = require("./routes/SharedRoutes");
const UnauthenticatedRoutes = require("./routes/UnauthenticatedRoutes");
const HttpError = require("./models/HttpError");
const verifyLogin = require("./controllers/Authentication/VerifyLoginMiddleware");
const app = express();
app.use(bodyParser.json());
app.use(fileUpload());
app.use("/uploads/notices", express.static(path.join("uploads", "notices")));
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
res.setHeader("Access-Control-Allow-Methods", "GET, POST, PATCH, DELETE");
next();
});
app.use("/api/auth", UnauthenticatedRoutes);
app.use(verifyLogin);
app.use("/api/student", StudentRoutes);
app.use("/api/teacher", TeacherRoutes);
app.use("/api/admin", AdminRoutes);
app.use("/api/shared", SharedRoutes);
app.use((req, res, next) => {
const error = new HttpError("Could not find this route.", 404);
throw error;
});
app.use((error, req, res, next) => {
if (req.file) {
fs.unlink(req.file.path, (err) => {
console.log(err);
});
}
if (res.headerSent) {
return next(error);
}
res.status(error.code || 500);
res.json({ message: error.message || "An unknown error occurred!" });
});
const server = app.listen(PORT, () => {
console.log(`server has started on port ${PORT}`);
});