-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
67 lines (55 loc) · 1.68 KB
/
index.ts
File metadata and controls
67 lines (55 loc) · 1.68 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const express = require("express");
const path = require('path');
const { exec } = require('node:child_process')
const os = require('os');
function getIPAddress() {
var interfaces = require('os').networkInterfaces();
for (var devName in interfaces) {
var iface = interfaces[devName];
for (var i = 0; i < iface.length; i++) {
var alias = iface[i];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal)
return alias.address;
}
}
return '0.0.0.0';
}
const app = express();
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
const ip = getIPAddress();
const port = 3000;
app.get("/", function (req, res) {
res.render('index.ejs',{hostname:ip});
});
app.post("/On", function (req, res) {
// run the `ls` command using exec
exec('vcgencmd display_power 1', (err, output) => {
// once the command has completed, the callback function is called
if (err) {
// log and return if we encounter an error
console.error("could not execute command: ", err)
return
}
// log the output received from the command
console.log("Output: \n", output)
})
res.send("On");
});
app.post("/Off", function (req, res) {
// run the `ls` command using exec
exec('vcgencmd display_power 0', (err, output) => {
// once the command has completed, the callback function is called
if (err) {
// log and return if we encounter an error
console.error("could not execute command: ", err)
return
}
// log the output received from the command
console.log("Output: \n", output)
})
res.send("Off");
});
app.listen(port, function () {
console.log(`Example app listening on port ${port}!`);
});