Premium Only Content
NodeJS HTTP Server Controlling A Relay With A Hold Button
CODE:
const http = require('http');//require HTTP
const querystring = require('querystring');//require query string
const url = require('url');//require url
const { exec } = require("child_process");//require EXEC
http.createServer((req, res) => {//CREATE WEB SERVER
let runcommand = (command) => {//CREATE A FUNCTION THAT CAN BE CALLED TO RUN COMMANDS
exec(`${command}`, (error, stdout, stderr) => {
if (error) {///CONSOLE LOG ERRORS
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
});
}
let htmlMessage = '';//THIS VARIABLE WILL CONTAIN THE MESSAGE THAT WILL BE DISPLAYED TO THE USERS
let getValues = querystring.parse(url.parse(req.url).query);
if(getValues.onoff == "on")//IF /?onoff=on is sent then run the code in the if block that will call the runcommand function
{
runcommand('echo "The pin was turned on "`date` >> ./log');//THIS COMMAND WILL LOG THE ACTION TAKEN USING THE ECHO COMMAND
runcommand('sudo pon 255');//THIS COMMAND WILL TURN ON THE parallel port
htmlMessage = "<h1>The pin was turned on!</h1>";//THIS IS THE MESSAGE THAT WILL BE SENT TO THE USER
} else if(getValues.onoff == "off"){//IF /?onoff=off is sent then run the code in the if block that will call the runcommand function
runcommand('echo "The pin was turned off "`date` >> ./log');//LOG THE ACTION WITH THE ECHO COMMAND
runcommand('sudo pon 0');//THIS COMMAND WILL TURN OFF THE parallel port
htmlMessage = "<h1>The pin was turned off!</h1>";//THIS IS THE MESSAGE THAT WILL BE SENT TO THE USER
}
res.setHeader('Content-Type', 'text/html');//SET THE HEAD FOR HTML
res.writeHead(200, { 'Content-Type': 'text/html' });//WRITE THE HEAD
console.log(htmlMessage);
//BELOW IS THE SIMPLE HTML IT ONLY HAS TWO LINKS, A H1 ELEMENT AND A LITTLE CSS. THE LINKS WILL SEND THE GET INFO TO TURN ON AND OFF THE PINS
let html = `
<!DOCTYPE html>
<html>
<head>
<style>
input {
display: block;
padding: 30px;
margin: 30px;
size: 90px;
background-color: pink;
}
</style>
</head>
<script>
let hold = () => {
console.log("HOLD ON");
fetch("./?onoff=on");
}
let up = () => {
console.log("Let up");
fetch("./?onoff=off");
}
</script>
<body>
${htmlMessage}
<input type="button"
onmousedown="hold()"
ontouchstart="hold()"
onmouseup="up()"
ontouchend="up()"
value="HOLD ON"
/>
<input type="button" onclick="hold()" value="ON" />
<input type="button" onclick="up()" value="OFF" />
</body>
</html>
`;
console.log("Running port 3000");
res.write(html);//WRITE THE HTML
res.end();//END
}).listen(3000);
-
LIVE
Inverted World Live
4 hours agoThe Titanic, The Gold Standard, and Jekyll Island | Ep. 129
4,021 watching -
2:56:44
TimcastIRL
3 hours agoNBA Games RIGGED, 34 Indictments, Democrat Calls It TRUMP'S REVENGE | Timcast IRL
204K88 -
LIVE
Laura Loomer
3 hours agoEP152: Texas Man Arrested For Threatening To Kill Laura Loomer
798 watching -
DVR
Man in America
6 hours agoEXPOSED: What the Vatican, CIA, & Elites Are HIDING About True Human Potential
18.9K10 -
Barry Cunningham
4 hours agoJOIN US FOR MOVIE NIGHT! TONIGHT WE FEATURE THE MOVIE RFK LEGACY!
39.7K14 -
1:13:42
Sarah Westall
4 hours agoHow Bitcoin was Hijacked, Palantir is a Deep State Upgrade & more w/ Aaron Day
19.1K4 -
15:59
ArynneWexler
6 hours agoAll The Reasons You're Right to Fear Zohran Mamdani | NN6
2.32K1 -
LIVE
Side Scrollers Podcast
11 hours ago🔴FIRST EVER RUMBLE SUB-A-THON🔴DAY 4🔴BLABS VS STREET FIGHTER!
901 watching -
LIVE
DLDAfterDark
3 hours agoGlock's Decision - How Could It Impact The Industry?
210 watching -
25:57
The Kevin Trudeau Show Limitless
1 day agoThe Sound Of Control: This Is How They Program You
32.8K8