Added api prefix

This commit is contained in:
Felix Albrigtsen 2022-03-21 09:28:26 +01:00
parent 906a7ada40
commit 53f166bb3d
1 changed files with 7 additions and 5 deletions

View File

@ -14,23 +14,25 @@ app.listen(port, () => {
}) })
app.use(express.json()); app.use(express.json());
app.use(express.urlencoded({ extended: true })); app.use(express.urlencoded({ extended: true }));
let api = express.Router();
app.use("/api", api);
// #endregion // #endregion
// #region frontend // #region frontend
// Serve static files from the React app // Serve static files from the React app
app.get("/", (req, res) => { api.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public", "landing.html")); res.sendFile(path.join(__dirname, "public", "landing.html"));
}); });
// #endregion // #endregion
// #region API // #region API
app.get("/tournament/getTournaments", (req, res) => { api.get("/tournament/getTournaments", (req, res) => {
tmdb.getTournaments() tmdb.getTournaments()
.then(tournaments => {res.json({"status": "OK", "data": tournaments}); }) .then(tournaments => {res.json({"status": "OK", "data": tournaments}); })
.catch(err => {res.json({"status": "error", "data": err}); }); .catch(err => {res.json({"status": "error", "data": err}); });
}); });
app.get("/tournament/:tournamentId/getMatches", (req, res) => { api.get("/tournament/:tournamentId/getMatches", (req, res) => {
let tournamentId = req.params.tournamentId; let tournamentId = req.params.tournamentId;
if (isNaN(tournamentId)) { if (isNaN(tournamentId)) {
res.json({"status": "error", "data": "tournamentId must be a number"}); res.json({"status": "error", "data": "tournamentId must be a number"});
@ -42,7 +44,7 @@ app.get("/tournament/:tournamentId/getMatches", (req, res) => {
.catch(err => res.send({"status": "error", "data": err})); .catch(err => res.send({"status": "error", "data": err}));
}); });
app.get("/match/:matchId/getMatch", (req, res) => { api.get("/match/:matchId/getMatch", (req, res) => {
let matchId = req.params.matchId; let matchId = req.params.matchId;
if (isNaN(matchId)) { if (isNaN(matchId)) {
res.json({"status": "error", "data": "matchId must be a number"}); res.json({"status": "error", "data": "matchId must be a number"});
@ -55,7 +57,7 @@ app.get("/match/:matchId/getMatch", (req, res) => {
}); });
// JSON body: {"winner": "teamId"} // JSON body: {"winner": "teamId"}
app.post("/match/:matchId/setWinner", (req, res) => { api.post("/match/:matchId/setWinner", (req, res) => {
let matchId = req.params.matchId; let matchId = req.params.matchId;
let winnerId = req.body.winnerId; let winnerId = req.body.winnerId;
if (isNaN(matchId)) { if (isNaN(matchId)) {