-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
46 lines (35 loc) · 1.19 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
var express = require('express');
var bodyParser = require('body-parser')
var server = express();
var fs = require("fs");
const FILENAME = 'lightSwitchBool.txt';
const port = 8000;
server.use( bodyParser.json() ); // to support JSON-encoded bodies
server.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
server.get('/switchStatus', function (req, res) {
fs.readFile(FILENAME, 'utf8', function (err, data) {
console.log(data);
res.end(data);
})
})
server.post('/setStatus', async function (req, res) {
let setStatus = req.body.switch
console.log(setStatus)
fs.writeFileSync(FILENAME, setStatus.toString());
res.send(setStatus);
})
server.post('/switchStatus', async function (req, res) {
const fileContents = fs.readFileSync(FILENAME, 'utf8');
console.log(fileContents);
let switchBool = fileContents == 'true';
switchBool = (!switchBool).toString();
fs.writeFileSync(FILENAME, switchBool);
res.send(switchBool);
})
var server = server.listen(port, function () {
var host = server.address().address
var port = server.address().port
console.log("Example server listening at %s", port)
})