-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
118 lines (98 loc) · 2.65 KB
/
app.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import express, { Request, Response, NextFunction } from "express";
import cors from "cors";
import { startServer } from "./Utils";
import Auth from "./Routes/Auth";
import publicSupport from "./Routes/publicSupport";
import Event from "./Routes/Event";
import Category from "./Routes/Category";
import cookieParser from "cookie-parser";
import Config from "./Config";
import path from "path";
import Demo from "./Routes/Demo";
import Payments from "./Routes/Payments";
// Server Initialization
const app = express();
const corsOrigin: string = Config.ORIGIN as string;
/**
* Configure and initialize the Express server.
*/
app.use(
cors({
credentials: true,
origin: [corsOrigin],
})
);
app.disable('x-powered-by');
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
// Serve the 'public' folder under the '/public' endpoint
const publicFolderPath = path.join(__dirname, "public");
app.use("/public", express.static("./public"));
// API routes start here
/**
* Default middleware for handling CORS headers.
*/
app.use((req: Request, res: Response, next: NextFunction) => {
res.header("Access-Control-Allow-Origin", corsOrigin);
res.header("Access-Control-Allow-Headers", "Origin,X-Requested-With, Content-Type, Accept");
next();
});
/**
* Default route providing information about the server.
*/
app.get("/", (req: Request, res: Response, next: NextFunction) => {
res.send({
data: {
appName: "Starter Pack | Backend",
developedBy: "Aditya Choudhury",
maintainedBy: "Aditya Choudhury",
version: "1.0.0.0",
},
success: true,
});
});
/**
* Health check API endpoint to verify if the server is up and running.
*/
app.get("/health", (req: Request, res: Response) => {
return res.status(200).json({
status: 200,
message: "Server is up and running"
});
});
// App Routes
/**
* Authentication API routes.
*/
app.use("/api/auth", Auth);
/**
* Public support API routes.
*/
app.use("/api/pr", publicSupport);
/**
* Events API routes.
*/
app.use("/api/event", Event);
/**
* Category API routes.
*/
app.use("/api/category", Category);
app.use("/api/demo", Demo);
app.use("/api/payments", Payments);
// Default not-found route
/**
* Default middleware for handling not-found routes.
*/
app.use((req: Request, res: Response, next: NextFunction) => {
res.send({
reason: "invalid-request",
message:
"The endpoint you want to reach is not available! Please check the endpoint again",
success: false,
});
});
/**
* Start the Express server.
*/
startServer(app);