-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·69 lines (61 loc) · 2.22 KB
/
app.js
File metadata and controls
executable file
·69 lines (61 loc) · 2.22 KB
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
import cookieParser from "cookie-parser";
import express from "express";
import { connectDatabase } from "./config/db.js";
import clubRouter from "./routes/club.routes.js";
import matchRouter from "./routes/match.routes.js";
import playerRouter from "./routes/player.routes.js";
import roundRouter from "./routes/round.routes.js";
import stageRouter from "./routes/stage.routes.js";
import stageItemRouter from "./routes/stageItem.routes.js";
import teamRouter from "./routes/team.routes.js";
import tournamentsRouter from "./routes/tournaments.routes.js";
import userRouter from "./routes/user.route.js";
const PORT = Number(process.env.PORT) || 3912;
if (isNaN(PORT) || !Number.isInteger(PORT)) {
throw new Error("Invalid PORT number specified");
}
const app = express();
app.use((req, res, next) => {
console.log(req.method, req.hostname, req.path);
next();
});
const FRONTEND_ORIGIN = process.env.FRONTEND_ORIGIN || "*";
// the cors plugin felt messier to configure, so here is manual:
app.use((req, res, next) => {
if (FRONTEND_ORIGIN === "*" || req.headers.origin === FRONTEND_ORIGIN) {
res.setHeader("Access-Control-Allow-Origin", FRONTEND_ORIGIN);
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader(
"Access-Control-Allow-Methods",
"GET,POST,PATCH,PUT,DELETE,OPTIONS",
);
res.setHeader(
"Access-Control-Allow-Headers",
"Content-Type,Authorization",
);
}
if (req.method === "OPTIONS") {
res.setHeader("Access-Control-Max-Age", "86400"); // cache pre-flight for 24h
res.sendStatus(204);
return;
}
next();
});
app.use(cookieParser());
app.use(express.json());
app.get("/", (req, res) => {
res.status(200).json({ status: "active" });
});
app.use("/user", userRouter);
app.use("/club", clubRouter);
app.use("/tournaments", tournamentsRouter);
app.use("/stages", stageRouter);
app.use("/rounds", roundRouter);
app.use("/team", teamRouter);
app.use("/match", matchRouter);
app.use("/stageItem", stageItemRouter);
app.use("/player", playerRouter);
await connectDatabase();
app.listen(PORT, "0.0.0.0", () => {
console.log(`Server running on port ${PORT} at http://localhost:${PORT}`);
});