diff --git a/backend/src/api.js b/backend/src/api.js index 6926720..22d512a 100644 --- a/backend/src/api.js +++ b/backend/src/api.js @@ -1,4 +1,8 @@ import { Router } from "express"; +import dotenv from 'dotenv'; +dotenv.config(); + +const db = await import('./db.js'); // dynamic import because otherwise dotenv will not work correctly const router = Router(); @@ -7,13 +11,14 @@ router.get("/healthcheck", (req, res) => { }); router.post("/scores", async (req, res) => { + console.log(req.body) const { name, game, score } = req.body; if (!name || !game || !Number.isFinite(score) || !Number.isInteger(score) || score < 0) { return res.status(400).json({ error: "Missing or invalid parameters" }); } - const conn = await pool.getConnection(); + const conn = await db.pool.getConnection(); try { await conn.beginTransaction(); @@ -54,7 +59,7 @@ router.get("/leaderboard/:game", async (req, res) => { const gameName = req.params.game; try { - const [rows] = await pool.query( + const [rows] = await db.pool.query( ` SELECT u.name AS user, s.score, s.created_at FROM scores s diff --git a/backend/src/index.js b/backend/src/index.js index 4f0422c..a847ae4 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -12,11 +12,13 @@ const app = express(); db.initDB(); -app.use(express.static(path.join(__dirname, "public"), { - dotfiles: "ignore" -})); +app.use(express.json()) +app.use(express.urlencoded({ extended: true })) +//app.use(express.static(path.join(__dirname, "public"), { +// dotfiles: "ignore" +//})); app.use("/api", apiRouter); -const server = app.listen(0, () => { +const server = app.listen(5555, () => { console.log(`Server running on http://localhost:${server.address().port}`); }); \ No newline at end of file