我可以用 Javascript 在虚拟主机的计算机上写入文件吗?

Can I write a file on the webhost's computer with Javascript?

我正在尝试制作一款在线游戏,我在 repl.it 上使用 JavaScript,我想在玩家输球时保存他们的最高分。我有一个名为 high_scores.txt 的文件,它与我的 script.js 文件位于同一目录中,我想写玩家在文件中的分数,如果它高于另一个分数。我一直在阅读,似乎您无法将文件写入其他计算机,但我正在尝试在托管网站的计算机上写入文件。有人知道怎么做吗?

Example: player's score = 145, files reads [563, 200, 76], this should write the player's score and make it [563, 200, 145] because it was bigger than that one

Or... player's score = 145, files reads [563, 200, 76], this should write the player's score and make it [563, 200, 76, 145]

我只是需要一些读写文件的方法。

也不是完全不可能(saving files, retrieving files),但是非常麻烦。

对于您要实现的目标,读取和保存本地存储会容易得多,它通过用户的浏览器持久保存数据:

const oldHighScores = JSON.parse(localStorage.highScores || '[]');
// ... get newScore
const newHighScores = [...oldHighScores, newScore].sort((a, b) => a - b).slice(1);
localStorage.highScores = JSON.stringify(newHighScores);