I’m working on a game and want to save user button click and scores data on the server to be accessible at the address http://localhost:3000/Custard/data/{playerName}. The idea is that players can view their score in comparison to other players, and that they can navigate to other players routes to view the data. The data is stored in JSON format.
My code is buggy and I keep triggering either a 404 error or a 500 ECONNREFUSED internal Server Error. Also, when I navigate to http://localhost:3000/Custard/data/Pudding for example, all I get it is the game screen, not an empty page and not any results.
I’ve included my server-side and parts of client-side index.js files.
Server-Side index.js:
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(express.json());
const code = "code";
app.post(`/Custard/data/${code}`, (req, res) => {
try {
const data = dateien;
console.log('Received data:', data);
// Process the data as needed
res.status(200).json({ message: 'Data received successfully', data: data });
} catch (error) {
console.error('Error processing data:', error);
res.status(500).json({ message: 'Internal Server Error' });
}
});
app.listen(3001, () => {
console.log('Server is running on port 3000');
});
module.exports = app;
Client-Side index.js:
function sendDataToServer(code, button1Val, button1Col, button2Val, button2Col,RT, pb, buttonAction) {
var code = document.getElementById('game-userName').innerHTML;
var pb = pb.innerHTML;
const dateien = {
code,
button1Val,
button1Col,
button2Val,
button2Col,
RT,
pb,
action: buttonAction,
};
fetch(`http://localhost:3000/Custard/data/${code}`, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(dateien)
})
.then(response => {
if (!response.ok) {
throw new Error('Nope -- Network response was not ok');
}
return response.json();
})
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
Client-Side package.json snippet:
"name": "client",
"version": "0.1.0",
"private": false,
"proxy": "http://localhost:3000",
"type": "module",
Thanks in advance!
I tried changing the proxy settings for the client package.json, I also tried different routes, all to no avail.