The code not working as expected
this function has been flagged:
function launchWord() {
fetch('/word')
.then(response => response.text())
.then(word => {
const x = Math.random() * (canvas.width - 100);
const y = Math.random() * (canvas.height - 30);
const direction = getRandomDirection();
const bgColor = getRandomColor();
const textColor = getContrastingColor(bgColor);
const bouncingWord = new BouncingWord(word, x, y, direction.dx, direction.dy, bgColor, textColor);
words.push(bouncingWord);
});
}
This occurs on the client side of the code.
The original call on the server side is here;
app.get('/word', (req, res) => {
const word = words[Math.floor(Math.random() * words.length)];
res.json({ word });
});
app.listen(PORT, () => console.log(`Server running at http://localhost:${PORT}`));
}
The code is supposed to fetch and display a random word but is displaying an error code instead
I am pretty sure the problem lies with how this is called fetch(‘/word’) but I do not know how to make the code work.
This is the console output:
script.js:77
GET http://127.0.0.1:5501/word 404 (Not Found)
launchWord @ script.js:77
(anonymous) @ script.js:105
Text should be displayed where the error codes appear. This is what the output looks like canvas boxes with error codes instead of words
user26414389 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3