-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
67 lines (55 loc) · 1.83 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
import express, { Application, Request, Response } from "express";
import cors from "cors";
import mongoose from "mongoose";
import dotenv from "dotenv";
import { attendanceRouter } from "./routes/attendance";
import { chatRouter } from "./routes/chat";
import { friendRouter } from "./routes/friend";
import { messageRouter } from "./routes/message";
import { postRouter } from "./routes/post";
import { reportRouter } from "./routes/report";
import { userRouter } from "./routes/user";
import { authRouter } from "./routes/auth";
dotenv.config();
mongoose.connect(process.env.MONGO_URI || "");
if (process.env.NODE_ENV !== "test") {
mongoose.connection.on("connected", () => {
console.log("mongo connected");
});
}
const app: Application = express();
const PORT: number = parseInt(process.env.PORT || "3001");
const corsOptions = {
origin: "*",
optionsSuccessStatus: 200,
allowedHeaders: "*",
};
app.use(cors(corsOptions));
app.options("*", cors(corsOptions));
app.use((req, res, next) => {
// Allow unauthenticated access to the OPTIONS method
if (req.method === "OPTIONS") {
return cors(corsOptions)(req, res, next);
}
// Continue with authentication for other methods
next();
});
app.use(express.json()); // Parses incoming JSON requests and puts the parsed data in req
app.use(express.urlencoded({ extended: true }));
app.get("/", (req: Request, res: Response) => {
res.status(200).json({ status: "healthy" });
});
if (process.env.NODE_ENV !== "test") {
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
}
app.use("/attendance", attendanceRouter);
app.use("/chat", chatRouter);
app.use("/friend", friendRouter);
app.use("/message", messageRouter);
app.use("/post", postRouter);
app.use("/report", reportRouter);
app.use("/user", userRouter);
app.use("/auth", authRouter);
export default app;