尝试通过我的树莓派上托管的网络服务器编辑文本文件。树莓派是一个热点,我用笔记本电脑连接到它
所以我有一个运行树莓派 3 的树莓派操作系统(牛眼)。我正在用 apache 运行一个网络服务器。覆盆子被设置为热点。我用笔记本电脑连接到它。我制作了一个可点击的框并在 javascript 中运行一个函数。我想要这个函数来编辑树莓派上的文本文件。我找到了一些有关node.js的信息并尝试以这种方式读取和写入文件。在终端中运行节点“文件名”.js 时它可以工作,但当我按下该框时我无法让它工作。这是我遇到的问题。我希望它在按下该框时编辑文本文件,而不仅仅是在终端中使用节点“文件名”.js 时。
我读到一些关于 require('fs') 在本地不起作用的内容,我不知道如何解决这个问题。
function forwardf() {
// Läs om variabeln är true eller false
alert("hit kommer vi");
document.getElementById("variabler.txt");
var file = this.files[0];
var reader = new FileReader();
alert("hit kommer vi");
// Entire file
//alert(this.result);
// By lines
var lines = this.result('\n');
for(var line = 0; line < lines.length; line++){
alert(lines[line]);
}
}
reader.readAsText(file);
alert(text);
/*
//const fs = require('fs');
//let fileString = fs.readFileSync("variabler.txt").toString();
const line = fileString.split(/\r?\n/);
const our_variable = line[0].split("=");
let temp = our_variable[1];
// Change this_var to true
if (temp == "false") {
//alert("temp was false");
console.log("temp was false" + "\n");
fileString = fileString.replace(/goforward=false/, "goforward=true");
}
// else change this_var to false
else {
//alert("temp was true");
console.log("temp was true" + "\n");
fileString = fileString.replace(/goforward=true/, "goforward=false");
}
fs.writeFileSync("variabler.txt", fileString);
console.log(fileString);
}
*/
.allt {
width:1200px;
height:700px;
border:1px solid black;
padding:3px;
display:inline-flex;
}
.box {
width:796px;
height:690px;
border:1px solid black;
}
.karta {
width:460px;
height:460px;
border:1px solid black;
display:inline-block;
margin:5px 0px 0px 5px;
text-align: center;
float:left;
}
.autonomt {
width:313px;
height:460px;
border:1px solid black;
display:inline-block;
text-align: center;
margin:5px 5px 5px 5px;
}
.auto {
width:290px;
height:220px;
border:1px solid black;
margin:5px;
}
.status {
width:290px;
height:220px;
border:1px solid black;
margin:5px;
}
.konsol {
Width:780px;
height:200px;
border:1px solid black;
margin:5px 0px 0px 5px;
text-align:center;
}
.movementbox {
width:386px;
height:680px;
border:1px solid black;
float:right;
margin: 5px;
}
.dartbox {
width:100px;
height:100px;
border:1px solid white;
display:inline-block;
margin:4px 5px;
text-align: center;
}
.image {
width:100%;
height:100%;
object-fit:scale-down;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<link rel="stylesheet" type="text/css" href="forsta_utkast_styles.css" media="screen" />
<script type="text/javascript"
src="funktioner.js";>
</script>
</head>
<body>
<div id="allt" class="allt">
<div id="box" class="box">
<div id="karta" class="karta">
<h2>Karta</h2>
</div>
<div id="autonomt" class="autonomt">
<div id="auto" class="auto">
<h2>Autonomt</h2>
</div>
<div id="status" class="status">
<p>b</p>
<p>b</p>
<p>b</p>
<p>b</p>
</div>
</div>
<div id="konsol" class="konsol">
<h1>konsol</h1>
</div>
</div>
<div id="movementbox" class="movementbox">
<div id="dartbox1" class="dartbox"><img src="bilder_pilar/forwardpil.png" alt="fel"></div>
<div id="dartbox2" class="dartbox"><img src="bilder_pilar/forwardpil.png" alt="fel"></div>
<div id="dartbox3" class="dartbox"><img src="bilder_pilar/forwardpil.png" alt="fel"></div>
<div id="dartbox4" class="dartbox"><img src="bilder_pilar/forwardpil.png" alt="fel"></div>
<div id="dartbox5" class="dartbox"><img src="bilder_pilar/forwardpil.png" alt="fel"></div>
<div id="dartbox6" class="dartbox"><img src="bilder_pilar/forwardpil.png" alt="fel"></div>
<div id="dartbox7" class="dartbox"><img src="bilder_pilar/forwardpil.png" alt="fel"></div>
<div id="dartbox8" class="dartbox"><img src="bilder_pilar/forwardpil.png" alt="fel"></div>
<div id="dartbox9" class="dartbox"><img src="bilder_pilar/forwardpil.png" alt="fel"></div>
<div id="dartbox10" class="dartbox"><img src="bilder_pilar/counter_clockwise.png" alt="fel"></div>
<div id="forwardbox" class="dartbox" onclick="forwardf()" style="cursor:pointer;"><img src="bilder_pilar/forwardpil.png" alt="fel"></div>
<div id="dartbox12" class="dartbox"><img src="bilder_pilar/clockwise.png" alt="fel"></div>
<div id="dartbox13" class="dartbox"><img src="bilder_pilar/leftpil.png" alt="fel"></div>
<div id="dartbox14" class="dartbox"><img src="bilder_pilar/manuell_styrning.png" alt="fel"></div>
<div id="dartbox15" class="dartbox"><img src="bilder_pilar/rightpil.png" alt="fel"></div>
<div id="dartbox16" class="dartbox"><img src="bilder_pilar/land.png" alt="fel"></div>
<div id="dartbox17" class="dartbox"><img src="bilder_pilar/backwardpil.png" alt="fel"></div>
<div id="dartbox18" class="dartbox"><img src="bilder_pilar/lift.png" alt="fel"></div>
</div>
</div>
</body>
</html>
So i have a raspberry 3 running raspberry pi os (bullseye). i am running a webserver on it with apache. The raspberry is set up as a hotspot. I am connected to it with my laptop. I have made a box which is clickable and runs a function in javascript. I want this funtion to edit a textfile which is on the raspberry. I found some information about node.js and tried reading and writing the file this way. it works when running node 'filename'.js in terminal but i cant get it to work when i press the box. This is the problem i have. i want it to edit the textfile when i press the box and not just when i use node 'filename'.js in terminal.
i read something about require('fs') doesnt work locally and i dont know how to solve this.
function forwardf() {
// Läs om variabeln är true eller false
alert("hit kommer vi");
document.getElementById("variabler.txt");
var file = this.files[0];
var reader = new FileReader();
alert("hit kommer vi");
// Entire file
//alert(this.result);
// By lines
var lines = this.result('\n');
for(var line = 0; line < lines.length; line++){
alert(lines[line]);
}
}
reader.readAsText(file);
alert(text);
/*
//const fs = require('fs');
//let fileString = fs.readFileSync("variabler.txt").toString();
const line = fileString.split(/\r?\n/);
const our_variable = line[0].split("=");
let temp = our_variable[1];
// Change this_var to true
if (temp == "false") {
//alert("temp was false");
console.log("temp was false" + "\n");
fileString = fileString.replace(/goforward=false/, "goforward=true");
}
// else change this_var to false
else {
//alert("temp was true");
console.log("temp was true" + "\n");
fileString = fileString.replace(/goforward=true/, "goforward=false");
}
fs.writeFileSync("variabler.txt", fileString);
console.log(fileString);
}
*/
.allt {
width:1200px;
height:700px;
border:1px solid black;
padding:3px;
display:inline-flex;
}
.box {
width:796px;
height:690px;
border:1px solid black;
}
.karta {
width:460px;
height:460px;
border:1px solid black;
display:inline-block;
margin:5px 0px 0px 5px;
text-align: center;
float:left;
}
.autonomt {
width:313px;
height:460px;
border:1px solid black;
display:inline-block;
text-align: center;
margin:5px 5px 5px 5px;
}
.auto {
width:290px;
height:220px;
border:1px solid black;
margin:5px;
}
.status {
width:290px;
height:220px;
border:1px solid black;
margin:5px;
}
.konsol {
Width:780px;
height:200px;
border:1px solid black;
margin:5px 0px 0px 5px;
text-align:center;
}
.movementbox {
width:386px;
height:680px;
border:1px solid black;
float:right;
margin: 5px;
}
.dartbox {
width:100px;
height:100px;
border:1px solid white;
display:inline-block;
margin:4px 5px;
text-align: center;
}
.image {
width:100%;
height:100%;
object-fit:scale-down;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<link rel="stylesheet" type="text/css" href="forsta_utkast_styles.css" media="screen" />
<script type="text/javascript"
src="funktioner.js";>
</script>
</head>
<body>
<div id="allt" class="allt">
<div id="box" class="box">
<div id="karta" class="karta">
<h2>Karta</h2>
</div>
<div id="autonomt" class="autonomt">
<div id="auto" class="auto">
<h2>Autonomt</h2>
</div>
<div id="status" class="status">
<p>b</p>
<p>b</p>
<p>b</p>
<p>b</p>
</div>
</div>
<div id="konsol" class="konsol">
<h1>konsol</h1>
</div>
</div>
<div id="movementbox" class="movementbox">
<div id="dartbox1" class="dartbox"><img src="bilder_pilar/forwardpil.png" alt="fel"></div>
<div id="dartbox2" class="dartbox"><img src="bilder_pilar/forwardpil.png" alt="fel"></div>
<div id="dartbox3" class="dartbox"><img src="bilder_pilar/forwardpil.png" alt="fel"></div>
<div id="dartbox4" class="dartbox"><img src="bilder_pilar/forwardpil.png" alt="fel"></div>
<div id="dartbox5" class="dartbox"><img src="bilder_pilar/forwardpil.png" alt="fel"></div>
<div id="dartbox6" class="dartbox"><img src="bilder_pilar/forwardpil.png" alt="fel"></div>
<div id="dartbox7" class="dartbox"><img src="bilder_pilar/forwardpil.png" alt="fel"></div>
<div id="dartbox8" class="dartbox"><img src="bilder_pilar/forwardpil.png" alt="fel"></div>
<div id="dartbox9" class="dartbox"><img src="bilder_pilar/forwardpil.png" alt="fel"></div>
<div id="dartbox10" class="dartbox"><img src="bilder_pilar/counter_clockwise.png" alt="fel"></div>
<div id="forwardbox" class="dartbox" onclick="forwardf()" style="cursor:pointer;"><img src="bilder_pilar/forwardpil.png" alt="fel"></div>
<div id="dartbox12" class="dartbox"><img src="bilder_pilar/clockwise.png" alt="fel"></div>
<div id="dartbox13" class="dartbox"><img src="bilder_pilar/leftpil.png" alt="fel"></div>
<div id="dartbox14" class="dartbox"><img src="bilder_pilar/manuell_styrning.png" alt="fel"></div>
<div id="dartbox15" class="dartbox"><img src="bilder_pilar/rightpil.png" alt="fel"></div>
<div id="dartbox16" class="dartbox"><img src="bilder_pilar/land.png" alt="fel"></div>
<div id="dartbox17" class="dartbox"><img src="bilder_pilar/backwardpil.png" alt="fel"></div>
<div id="dartbox18" class="dartbox"><img src="bilder_pilar/lift.png" alt="fel"></div>
</div>
</div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在,这是如何工作的。
这就是为什么它不起作用的原因: (不要忽略它,否则您不会理解任何一个)
这是您需要做的事情:
祝你好运。愉快的编码!
Thats now how any of it works.
Here's why its not working: (dont ignore this, else you wont understand any of it)
Here's what you need to do:
Good luck. Happy coding!