javascript - 如何通过 html“按钮单击”调用“JS 文件”并且 JS 文件正在使用 shell.js 运行 bash 脚本
问题描述
我想通过单击 html 按钮连接到“server.js”文件。
“server.js”文件正在使用 shell.js 运行“bash.sh”文件
任何人都可以就如何进行提供一些想法或指导吗?
按钮.html
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>
<script src="server.js"></script>
</body>
客户端.js
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}
服务器.js
const shell = require('shelljs');
shell.exec('./bash.sh')
bash.sh
#!/bin/bash
printf "command 1 is running...\n\n"
mlpack_linear_regression --training_file aircon.csv -v -M lr.xml
printf "\ncommand 2 is running...\n\n"
mlpack_linear_regression --training_file aircon.csv --test_file
predict.csv --output_predictions_file prediction.csv -v
printf "\nPredicted Output: \n\n"
cat prediction.csv # after this command the result should shown on
#the browser screen
echo "hello" >> file # To test if connection is happening or not
#by writing a string in a file
解决方案
查看shell.js的文档。它说:
Node.js 的便携式 Unix shell 命令
您正在尝试在网络浏览器中使用它。它不是为在 Web 浏览器中运行而设计的,因此无法在那里工作。
您需要使用 Node.js 运行它。
您可以使用 Node.js 编写一个 HTTP 服务器,然后使用 Ajax(或者只是一个常规的链接单击或表单提交)向该服务器发出一个 HTTP 请求,以使其通过 shell 执行您想要执行的任何操作。
推荐阅读
- java - 使用 Egit 推送时出错:“对等方重置连接:套接字写入错误”
- kubernetes - 在 k8s 中更新秘密后如何检查文件是否更新?
- java - 向下转换错误 (Java)。以下代码似乎不起作用,感谢任何解释
- ruby - 如何使用 rbenv 安装 Ruby 2.5.1
- node.js - 在 React 中显示来自 S3 的图像
- powershell - 使用 PowerShell 查询 Active Directory 并将数据添加到现有 csv 的新列
- java - Google Play 结算库 2.0 版 - .setSkuDetails(skuDetails)
- ruby-on-rails - 如何在 Ruby 中存根请求/响应
- javascript - 在网格中添加“mousedown”事件侦听器,除非发生“mouseup”事件,否则将触发所有单元格
- python - 试图从一本书的练习中修复一个错误