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
Timcast
27 minutes ago🚨PRESS CONFERENCE: Charlie Kirk Suspect In Custody | Tim Pool
11,675 watching -
LIVE
LFA TV
9 hours agoKILLER CAUGHT LIVE PRESSER! - FRIDAY 9/12/25
11,039 watching -
LIVE
iCkEdMeL
1 hour agoBREAKING: Trump Says Suspect in Charlie Kirk Assassination Likely in Custody
1,037 watching -
LIVE
Times Now World
7 hours agoLIVE NEWS | Charlie Kirk's Final Debate 24 Hours Before Tragic Murder By Sniper in Utah Event!
321 watching -
LIVE
The Bubba Army
23 hours agoVideo Killed The Radio Star, RELEASE DAY! - Bubba the Love Sponge® Show | 9/12/25
5,199 watching -
2:00:36
BEK TV
1 day agoTrent Loos in the Morning - 9/11/2025
12.6K1 -
22:25
DeVory Darkins
1 day ago $21.86 earnedTrump issues statement to the nation as FBI drops shocking update about shooter
99.1K143 -
10:55
Nate The Lawyer
2 days ago $2.11 earnedPortland's WOKE Laws Stop ALL Arrests to Help Illegal Migrants Fight ICE.
38.1K30 -
3:06:32
Price of Reason
16 hours agoCharlie Kirk Investigation Continues | Remembering 9-11 | Robert Downey Jr. Doctor Doom Look Reveal
281K71 -
2:12:26
Inverted World Live
12 hours agoManhunt for Suspect in Charlie Kirk's Assassination Continues, NASA Reveals "Life" on Mars | Ep. 107
264K58