fix: API works now and a fixt port was set
This commit is contained in:
@@ -1,4 +1,8 @@
|
|||||||
import { Router } from "express";
|
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();
|
const router = Router();
|
||||||
|
|
||||||
@@ -7,13 +11,14 @@ router.get("/healthcheck", (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.post("/scores", async (req, res) => {
|
router.post("/scores", async (req, res) => {
|
||||||
|
console.log(req.body)
|
||||||
const { name, game, score } = req.body;
|
const { name, game, score } = req.body;
|
||||||
|
|
||||||
if (!name || !game || !Number.isFinite(score) || !Number.isInteger(score) || score < 0) {
|
if (!name || !game || !Number.isFinite(score) || !Number.isInteger(score) || score < 0) {
|
||||||
return res.status(400).json({ error: "Missing or invalid parameters" });
|
return res.status(400).json({ error: "Missing or invalid parameters" });
|
||||||
}
|
}
|
||||||
|
|
||||||
const conn = await pool.getConnection();
|
const conn = await db.pool.getConnection();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await conn.beginTransaction();
|
await conn.beginTransaction();
|
||||||
@@ -54,7 +59,7 @@ router.get("/leaderboard/:game", async (req, res) => {
|
|||||||
const gameName = req.params.game;
|
const gameName = req.params.game;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const [rows] = await pool.query(
|
const [rows] = await db.pool.query(
|
||||||
`
|
`
|
||||||
SELECT u.name AS user, s.score, s.created_at
|
SELECT u.name AS user, s.score, s.created_at
|
||||||
FROM scores s
|
FROM scores s
|
||||||
|
|||||||
@@ -12,11 +12,13 @@ const app = express();
|
|||||||
|
|
||||||
db.initDB();
|
db.initDB();
|
||||||
|
|
||||||
app.use(express.static(path.join(__dirname, "public"), {
|
app.use(express.json())
|
||||||
dotfiles: "ignore"
|
app.use(express.urlencoded({ extended: true }))
|
||||||
}));
|
//app.use(express.static(path.join(__dirname, "public"), {
|
||||||
|
// dotfiles: "ignore"
|
||||||
|
//}));
|
||||||
app.use("/api", apiRouter);
|
app.use("/api", apiRouter);
|
||||||
|
|
||||||
const server = app.listen(0, () => {
|
const server = app.listen(5555, () => {
|
||||||
console.log(`Server running on http://localhost:${server.address().port}`);
|
console.log(`Server running on http://localhost:${server.address().port}`);
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user