使用 javascript 在服务器上编辑具有高分的文件

Editing a file with highscores on a server using javascript

提问人:Jaap van der Heijden 提问时间:11/8/2023 更新时间:11/8/2023 访问量:19

问:

要设置数独比赛,我们需要管理一个文本文件,其中包含高分、名称和可选的价格、电子邮件地址。此文件必须位于 Web 服务器上,我们尝试使用 javascript 访问它但没有成功。当我们询问AI(ChatGPT和Bard)时,他们推荐调用fs模块的Node.js模块。当我运行该脚本(如下)时,网络浏览器无法识别“require”函数。安装 require 函数并不是那么简单。有没有更简单的方法可以使用 javascript 在服务器上打开、编辑和保存文件?

这是用于打开、修改和保存高分文件的 AI 代码

<!DOCTYPE html>
<html>
<head>
    <title>File Reading, Editing and Writing</title>
</head>
<body>
    <script>
     const fs = require('fs');

    const filePath = '/path/to/Sudoku_highscores.txt';

    // Read the current contents of the file
    const fileContents = fs.readFileSync(filePath, 'utf8');

    // Modify the file contents
    const modifiedContents = fileContents.replace('old text', 'new text');

    // Write the modified contents to the file
    fs.writeFileSync(filePath, modifiedContents, 'utf8');
    </script>
</body>
</html>
JavaScript 文件 Web 服务器

评论

2赞 Stephen Ostermiller 11/8/2023
客户端 JavaScript 无法修改服务器上的任何内容,除非在服务器上运行某种允许它的应用程序。您应该为此编写一些服务器端代码。
0赞 freedomn-m 11/8/2023
推荐的 node.js 是在服务器上运行的服务器端组件 - node.js 不在浏览器中运行。

答: 暂无答案