forked from umeh2002/dirt2schoolBE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainApp.ts
70 lines (60 loc) · 1.76 KB
/
mainApp.ts
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
import cors from "cors";
import {
Application,
NextFunction,
Request,
Response,
json,
} from "express";
import helmet from "helmet";
import morgan from "morgan";
import user from "./router/authRouter";
import bag from "./router/bagRouter";
import fee from "./router/feeRouter";
import profileRouter from "./router/profileRouter";
import school from "./router/schoolRouter";
import { HTTP, mainError } from "./error/mainError";
import { errorHandler } from "./error/errorHandler";
const mainApp = (app: Application) => {
app.use(json());
app.use(cors());
app.use(helmet());
app.use(morgan("dev"));
app.set("view engine", "ejs");
app.use("/api", user);
app.use("/api", bag);
app.use("/api", fee);
app.use("/api", profileRouter);
app.use("/api", school);
app.use((req: any, res: Response, next: NextFunction) => {
res.header("Access-Control-Allow-Origin", "http://localhost:5173");
res.header("Access-Control-Allow-Credentials", "true");
res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
res.header("Access-Control-Allow-Headers", "Content-Type");
next();
});
app.get("/", (req: Request, res: Response) => {
try {
return res.status(HTTP.OK).json({
message: "Welcome to Dirt2School API, Enjoy 💥💫☑",
});
} catch (error: any) {
return res.status(HTTP.BAD).json({
message: "error",
data: error.message,
});
}
});
app.use("*", (req: Request, res: Response, next: NextFunction) => {
next(
new mainError({
name: "Route Error",
message: `Incorrect url ${req.originalUrl} does not exist`,
status: HTTP.BAD,
success: false,
})
);
});
app.use(errorHandler);
};
export default mainApp;