|
|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
const express = require('express');
|
|
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
app.use(express.json());
|
|
|
|
|
|
|
|
|
|
const sensors = [
|
|
|
|
|
{
|
|
|
|
|
@ -16,6 +16,8 @@ const sensors = [
|
|
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const measurements = []; //empty
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.get('/', (req, res) => {
|
|
|
|
|
res.send('Sensor Logger');
|
|
|
|
|
@ -42,8 +44,27 @@ app.get('/api/sensors/:id', (req, res) => {
|
|
|
|
|
res.json(sensor);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.post('/api/measurements', (req, res) => {
|
|
|
|
|
// crequire json submission
|
|
|
|
|
|
|
|
|
|
// 1. Receive the measurement.
|
|
|
|
|
const measurement = req.body;
|
|
|
|
|
|
|
|
|
|
// verify object
|
|
|
|
|
|
|
|
|
|
// Validate submitted fields and sensor id
|
|
|
|
|
|
|
|
|
|
// 2. Store it in memory.
|
|
|
|
|
measurements.push(measurement);
|
|
|
|
|
|
|
|
|
|
// 3. Show it in the server terminal.
|
|
|
|
|
console.log('Received:', measurement);
|
|
|
|
|
|
|
|
|
|
// 4. Confirm that it was stored.
|
|
|
|
|
res.status(201).json(measurement);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// starting the listening service
|
|
|
|
|
app.listen(3030, () => {
|
|
|
|
|
console.log('Server running at http://localhost:3030');
|
|
|
|
|
});
|
|
|
|
|
|