在AutoStart上运行时,NodeJS并不完全功能

发布于 2025-02-11 03:14:17 字数 1765 浏览 1 评论 0原文

我目前正在尝试使用nodejs使用两个HTML按钮(音量向上和音量)更改Raspberry Pi的音量。卷更改是通过在index.html中单击一个html-button时调用bash脚本来完成的。

手动启动节点时,

当我使用node server.js

一切正常,我可以使用两个按钮增加和减少音量。 一旦我将节点脚本放到autostart,只有“音量上”按钮可以正常工作,而音量下降似乎无法正常工作。

杀死自动节点过程并手动启动它,它可以再次工作。 结果如何不同,具体取决于节点的开始? 我的错误是什么?

我的节点脚本看起来像这样:

var express = require('express');
var path = require('path');
var open = require('open');
var fs = require('fs');
const { exec } = require('child_process');
var bodyParser = require('body-parser');

var port = 3000;
var app = express();

app.get('/', function(req, res){
    res.sendFile(path.join(__dirname, 'index.html'));
});

app.use(bodyParser.urlencoded({ extended: false }));

app.post('/', function(request, respond) {
    var inputValue = request.body.volume;

    if (inputValue == "up") {
    exec('sh volumeup.sh');
}
    if(inputValue == "down") {
    exec('sh volumedown.sh');
}
});

app.listen(port, function(err){
    if(err){
        console.log(err);
    }else{
        open('http://localhost:' + port);
    }
});

使用我的index.html:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8"/>
</head>
<body>
  <h2>Change Volume</h2>
  <form action="" method="post">
    <button name="volume" value="up">Volume Up</button>
    <button name="volume" value="down">Volume Down</button>
</form>
</body>
</html>

要让nodejs在autostart上运行,我已经设置了一个 *.desktop-file:

[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=<Node>
Comment=
Exec=/usr/bin/node /home/pi/Desktop/server.js
StartupNotify=false
Terminal=true
Hidden=false

非常感谢您,我非常感谢您的帮助!

I am currently trying to change the volume of my Raspberry Pi with two html buttons (volume up and volume down), using nodeJS. The volume change is done by calling a bash script when clicking on one of the html-buttons in index.html.

When I start node manually from terminal with

node server.js

everything works fine and I can increase and decrease the volume using the two buttons.
As soon as I put the node script to autostart, only the "volume up" button is working, while the volume down does not seem to be working.

Killing the autostarted node process and starting it manually, it works just fine again.
How can the outcome be this different, depending on how node is started here?
What is my error in this?

My node script looks like this:

var express = require('express');
var path = require('path');
var open = require('open');
var fs = require('fs');
const { exec } = require('child_process');
var bodyParser = require('body-parser');

var port = 3000;
var app = express();

app.get('/', function(req, res){
    res.sendFile(path.join(__dirname, 'index.html'));
});

app.use(bodyParser.urlencoded({ extended: false }));

app.post('/', function(request, respond) {
    var inputValue = request.body.volume;

    if (inputValue == "up") {
    exec('sh volumeup.sh');
}
    if(inputValue == "down") {
    exec('sh volumedown.sh');
}
});

app.listen(port, function(err){
    if(err){
        console.log(err);
    }else{
        open('http://localhost:' + port);
    }
});

with my index.html:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8"/>
</head>
<body>
  <h2>Change Volume</h2>
  <form action="" method="post">
    <button name="volume" value="up">Volume Up</button>
    <button name="volume" value="down">Volume Down</button>
</form>
</body>
</html>

To get the nodejs to run on autostart, I have set up a *.desktop-file:

[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=<Node>
Comment=
Exec=/usr/bin/node /home/pi/Desktop/server.js
StartupNotify=false
Terminal=true
Hidden=false

Thank you very much in advance, I really appreciate your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾冷 2025-02-18 03:14:17

我自己弄清楚。
如果任何人都面临类似的问题:对我有

的是,将两个外壳的文件名都放在他们的绝对路径中:

´´´´gied

app.post('/', function(request, respond) {
    var inputValue = request.body.volume;

    if (inputValue == "up") {
    exec('sh /home/pi/server/volumeup.sh');
}
    if(inputValue == "down") {
    exec('sh /home/pi/server/volumedown.sh');
}
}); 

帮助

我比我想承认的时间更长的时间:-)

Figured it out by myself.
In case anyone is facing a similar issue:

What helped me, was to put both shell file names into their absolute path:

´´´

app.post('/', function(request, respond) {
    var inputValue = request.body.volume;

    if (inputValue == "up") {
    exec('sh /home/pi/server/volumeup.sh');
}
    if(inputValue == "down") {
    exec('sh /home/pi/server/volumedown.sh');
}
}); 

´´´

Bothered me for longer than I want to admit :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文