Repository navigation
Expand file tree
/
Copy pathapi.js
More file actions
64 lines (51 loc) · 1.93 KB
/
Copy pathapi.js
File metadata and controls
64 lines (51 loc) · 1.93 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
require('dotenv').config();
const os = require('os');
const mongoose = require('mongoose');
const Sensor = require('./models/sensor');
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
const serveraddress = process.env.MONGO_URI;
if (!serveraddress) {
console.error('Missing MONGO_URI environment variable. Set it before starting the server.');
process.exit(1);
}
mongoose.connect(serveraddress);
// Used to identify which EC2 instance served a given request —
// makes it easy to prove the load balancer is actually alternating
// between your two instances in your evidence screenshots.
const hostname = os.hostname();
// all the sensor readings in MongoDB
app.get('/', async function (req, res) {
const filter = {}; // get all documents.
const all = await Sensor.find(filter);
res.send({ servedBy: hostname, data: all }); // send the documents + server identity back to the client.
});
// all the sensor readings with an ID in MongoDB
app.get('/:id', async function (req, res) {
const filter = { _id: req.params.id }; // get documents by ID.
const all = await Sensor.find(filter);
res.send({ servedBy: hostname, data: all });
});
// submit a new value
app.post('/', async function (req, res) {
// Generate a temperature value.
const low = 10;
const high = 40;
const reading = Math.floor(Math.random() * (high - low) + low);
// Create a new sensor reading.
const newSensor = new Sensor({
name: "temperaturesensor",
address: "221 Burwood Hwy, Burwood VIC 3125",
time: Date.now(),
temperature: reading
});
newSensor.save().then(doc => {
console.log("Saving Sensor reading to Database");
console.log(doc);
});
res.send({ servedBy: hostname, message: 'Added Data!' }); // return back to the client.
});
app.listen(port, () => {
console.log(`[${hostname}] listening on port ${port}`);
});