Skip to content

Commit 831d634

Browse files
authored
Updated app.js to toggle the light state with each call to /toggle.
1 parent eed7a67 commit 831d634

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

app.js

+19-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,29 @@ const app = express()
33
const port = 4000
44
//Tell Express to serve static html files from the 'public' folder.
55
app.use(express.static('public'))
6+
//Create a variable to store the state of the light (on or off) at any given time,
7+
//assume off (false) to start with
8+
var light_on = false;
69

7-
8-
//Configure express to listen for HTTP GET requests at "/"
9-
//(i.e. http://localhost:port"/") <= No need to include the "/" at the end here.
10-
app.get("/", (req, res) => {
10+
//Configure express to listen for HTTP GET requests at "/toggle"
11+
//(i.e. http://localhost:port"/toggle")
12+
app.get("/toggle", (req, res) => {
1113

1214
//Let the const 'spawn' hold the child_process module.
1315
const { spawn } = require("child_process");
14-
//Call the child_process to run the Python script - store the output in pyProg.
15-
const pyProg = spawn("python", ["./code/my_script.py"]);
16+
var pyProg;
17+
18+
//Toggle the light to the opposite value of whatever it is right now
19+
light_on = !light_on;
20+
21+
//Call the child_process to run the Python script to turn on or off the light
22+
//appropriately, depending on the current value of light_on - store the output in pyProg.
23+
if (light_on) {
24+
pyProg = spawn("python", ["./code/script_turn_on.py"]);
25+
}
26+
else {
27+
pyProg = spawn("python", ["./code/script_turn_off.py"]);
28+
}
1629

1730
//When output data (i.e. standard output) has been detected in 'pyProg', run
1831
//this function to handle that output - the variable 'data' holds the output.

0 commit comments

Comments
 (0)