提问人:Jaap van der Heijden 提问时间:11/8/2023 更新时间:11/8/2023 访问量:19
使用 javascript 在服务器上编辑具有高分的文件
Editing a file with highscores on a server using javascript
问:
要设置数独比赛,我们需要管理一个文本文件,其中包含高分、名称和可选的价格、电子邮件地址。此文件必须位于 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>
答: 暂无答案
评论