In your html-file you can create three different buttons with three differents ids
<button id="btn1"></button>
<button id="btn2"></button>
<button id="btn3"></button>
Then for your buttons make inside your js-file three different eventListeners
document.getElementById("btn1").addEventListener('click', function() {
const data = {value: 131};
const options = {
method: 'POST',
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data)
};
await fetch('/startMyFunction', options);
})
The same for the two other buttons with the different values
In your server side js file you have to create a function like this
myFunction(val) {
var buf = new Buffer(1024);
buf.writeInt32LE(val, 0); //max value to send 2147483647
// Assumes the connection is established
client.write(buf); //This will send the byte buffer over TCP
});
}
Also you have to listen on a path for example
app.post('/startMyFunction', (req, res) => {
// read the parameter out of the request body
const val = req.body.value
// Call the function with this parameter
myFunction(val);
});
The code snippets are (adapted) from my repository, there you can see the full code
https://github.com/CreaTorAlexander/corona-motivator
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…