如何通过 html "button-click" 调用 "JS file" 并且 JS 文件是 运行 使用 shell.js 的 bash 脚本

How to call a "JS file" by a html "button-click" and the JS file is running a bash script using shell.js

我想通过单击 html 按钮连接到 "server.js" 文件。

"server.js" 文件是 运行 一个使用 shell.js

的 "bash.sh" 文件

任何人都可以就如何进行提供一些想法或指导吗?

button.html

<!DOCTYPE html>
<html>
<body> 

<button onclick="myFunction()">Click me</button>

<p id="demo"></p>

<script src="server.js"></script>

</body>

client.js

function myFunction() {
      document.getElementById("demo").innerHTML = "Hello World";
    }

server.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 的文档。它说:

Portable Unix shell commands for Node.js

您正在尝试在网络浏览器中使用它。它不是为 运行 在网络浏览器中设计的,因此无法在其中工作。

您需要 运行 使用 Node.js。


可以 使用 Node.js 编写 HTTP 服务器,然后使用 Ajax(或者只是常规的 link 单击或表单提交) 向该服务器发出 HTTP 请求,使其执行您想要的任何操作,因此使用 shell.