-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
52 lines (46 loc) · 1.39 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const express = require("express")
const bodyParser = require("body-parser")
const { exec } = require('child_process');
const app = express();
const port = 7077;
app.use(bodyParser.json())
app.get("/api", (req, res) => {
res.sendFile(__dirname + "/index.html")
})
app.post("/api", (req, res) => {
console.log("POST request received" + JSON.stringify(req.body))
let ans = JSON.parse(JSON.stringify(req.body));
console.log(ans.status);
res.json({ message: "Light Swithced" })
if (ans.status) {
let on = exec("sh on.sh", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
// console.log(on);
} else {
console.log("OFF");
let off = exec("sh off.sh", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
// console.log(off);
}
})
app.listen(port, () => {
console.log("Server running on port " + port)
})