I am trying to restart or shutdown a Raspberry Pi from a Web Interface. I have managed this in the past by using PHP's shell_exec
.
This time I am trying to do so exclusively with JavaScript
.
For front-end I have chosen VueJS
. For back-end I have no idea what to choose; I have seen NodeJS
, Express JS
and a ton more servers capable of running on a UNIX OS device such as the Raspberry Pi.
I have managed to use child_process
and run shell commands from a test file using node./test.js
, but how can I do that as a whole?
How do I link a Restart Button
from Vue to a Node or Express Server ?
This is the Vue component that I came up with for testing. It contains an input, instead of a button, just to test simple shell commands.
<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>
I have not decided upon a server, but I have seen some examples of Express server.
child_process
is a node.js library and will not be available in the browser. You can either convert your application to an electron application where you can configure it to be available (and only use on the host you're executing on), or set up an API that your application can use to trigger the command.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.