-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
74 lines (66 loc) · 1.86 KB
/
index.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
import bodyParser from "body-parser";
import cookieParser from "cookie-parser";
import cors from "cors";
import dotenv from "dotenv";
import express from "express";
import "express-async-errors";
import morgan from "morgan";
import { useErrorHandler, useNotFound, useRateLimiter } from "./middlewares/";
import {
appointmentRouter,
authRouter,
hospitalRouter,
reviewRouter,
userRouter,
roomRouter,
medicalRecordRouter,
} from "./routes";
import { connectToDb } from "./utils";
import http from "http";
import { initSocket } from "./sockets/socket.server";
dotenv.config();
const PORT = process.env.PORT || 2800;
const app = express();
const server = http.createServer(app);
initSocket(server);
//middlewares
const allowedOriginPatterns = [
/https:\/\/getcaresync\.vercel\.app$/,
/https:\/\/getcaresync\.netlify\.app$/,
/https:\/\/caresync\.brimble\.app$/,
/http:\/\/localhost:3000$/,
];
const corsOptions = {
origin: (origin: any, callback: any) => {
// Check if the origin matches any of the patterns
if (
!origin ||
allowedOriginPatterns.some((pattern) => pattern.test(origin))
) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
},
credentials: true,
};
app.use(cors(corsOptions));
app.use(cookieParser());
app.use(bodyParser.json({ limit: "100mb" }));
app.use(morgan("dev"));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(useRateLimiter);
//endpoints
app.use("/api/auth", authRouter);
app.use("/api/user", userRouter);
app.use("/api/hospital", hospitalRouter);
app.use("/api/appointment", appointmentRouter);
app.use("/api/review", reviewRouter);
app.use("/api/room", roomRouter);
app.use("/api/medical-record", medicalRecordRouter);
app.use(useNotFound);
app.use(useErrorHandler);
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
connectToDb();
});