[英]How to execute sh script or shell commands from browser input using a node server?
我正在嘗試從 Web 接口重新啟動或關閉 Raspberry Pi。 我過去使用 PHP 的shell_exec
管理過這個問題。
這次我嘗試專門使用JavaScript
這樣做。
對於前端,我選擇VueJS
。 對於后端,我不知道該選擇什么; 我見過NodeJS
、 Express JS
和大量能夠在 UNIX OS 設備(例如 Raspberry Pi)上運行的服務器。
我已經設法使用child_process
並使用node./test.js
從測試文件中運行 shell 命令,但是我該怎么做呢?
如何將Restart Button
從 Vue 鏈接到節點或 Express 服務器?
這是我用來測試的 Vue 組件。 它包含一個輸入,而不是一個按鈕,只是為了測試簡單的 shell 命令。
<template>
<div id="wrapper">
<div class="input_row">
<input type="text" class="input_text" id="shell" v-model="shell_command" />
<label class="label_" for="shell">Type Shell Command</label>
</div>
<button type="button" class="button" @click="shellExec">Submit !</button>
</div>
</template>
<script>
import { exec } from "child_process";
export default {
name: "ShellExec",
components: {},
data() {
return {
shell_command: ""
};
},
methods: {
async shell() {
return new Promise((resolve, reject) => {
exec(this.shell_command, (err, stdout, stderr) => {
if (err) {
reject(err);
} else {
resolve({ stdout, stderr });
}
});
});
},
async shellExec() {
let { stdout } = await this.shell();
for (let line of stdout.split("\n")) {
console.log(`ls: ${line}`);
}
}
}
};
</script>
<style scoped>
</style>
我還沒有決定使用服務器,但我看過一些 Express 服務器的例子。
child_process
是一個 node.js 庫,在瀏覽器中不可用。 您可以將您的應用程序轉換為 electron 應用程序,您可以在其中將其配置為可用(並且僅在您正在執行的主機上使用),或者設置您的應用程序可用於觸發命令的 API。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.