In my game, I am encountering an issue where the score does not update within my get function every time the game is played. Instead, I keep receiving null property errors. Any assistance on resolving this would be highly appreciated.
I attempted to include the following line inside my get function
const scoreElement = document.getElementById("scoreNums");
scoreElement.innerText = score.correct + "/" + score.total;
This was done in an effort to dynamically change the displayed score after each playthrough.
Below is a snippet of my server-side JavaScript code:
const express = require("express");
const server = express();
const port = 3042;
server.use(express.json());
server.use(express.urlencoded({ extended: true }));
let score = 0;
let correct = 0;
// Middleware for handling cross-domain requests
const allowCrossDomain = function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,POST");
res.header("Access-Control-Allow-Headers", "Content-Type");
next();
};
server.use(allowCrossDomain);
// Route for handling POST requests
server.post("/myPost", function (req, res) {
score += req.body.score;
correct += req.body.correct;
let obj = {correct: correct, score: score};
return res.status(201).send(obj);
});
// Route for handling GET requests
server.get("/myGet", function (req, res) {
const scoreData = { correct: correct, score: score };
return res.status(200).send(scoreData);
});
server.listen(port, function () {
console.log("Listening on port 3042");
});
Further down the page, additional JavaScript functions are defined for different functionalities within the game such as audio playback, image display, drag and drop functionality, error handling, etc.