-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (50 loc) · 1.69 KB
/
index.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
53
54
55
56
57
58
59
60
61
62
63
64
65
const express = require("express");
const app = express();
const PORT = 3000;
//importing the model to create a new on_off_device
const OnOffDevice = require("./models/on_off_device.js");
// this is the list of devices
const devices = [];
// this is how to create a new on off based device
const newDevice = new OnOffDevice(devices.length, "Device 001");
//this is how to add your new device
devices.push(newDevice);
console.log(devices);
// this is a post request to the route "/:id"
// this route will be to get the object with the given id
app.get("/:id", (req, res) => {
//extracting the id form request parameters
const id = req.params.id;
//the first device is selected which has the mentioned id and is in the devices list
const device = devices.filter(item => item.id !== id);
if (device.length < 1)
res.status(404).json({
message: `Could not find the device with ID ${id}`
});
else {
res.status(200).json({
device: device[0]
});
}
});
//this is for turning a device on and off
app.get("/toggle_status/:id", (req, res) => {
//extracting the id form request parameters
const id = req.params.id;
//the first device is selected which has the mentioned id and is in the devices list
const device = devices.filter(item => item.id !== id);
if (device.length < 1)
res.status(404).json({
message: `Could not find the data of device with id ${id}`
});
else {
device[0].toggleStatus();
res.status(200).json({
id,
status: device[0].status
});
}
});
app.listen(PORT, () => {
console.log(`server running on localhost:${PORT}`);
})