You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.3 KiB
JavaScript
71 lines
1.3 KiB
JavaScript
const express = require('express');
|
|
const app = express();
|
|
app.use(express.json());
|
|
|
|
const sensors = [
|
|
{
|
|
id: 'htu21d-01',
|
|
model: 'HTU21D',
|
|
quantities: ['temperature', 'relative_humidity']
|
|
},
|
|
{
|
|
id: 'htu21d-02',
|
|
model: 'HTU21D',
|
|
quantities: ['temperature', 'relative_humidity']
|
|
},
|
|
|
|
];
|
|
|
|
const measurements = []; //empty
|
|
|
|
|
|
app.get('/', (req, res) => {
|
|
res.send('Sensor Logger');
|
|
});
|
|
|
|
app.get('/api/status', (req, res) => {
|
|
res.json({ status: 'online', application: 'sensor-logger' });
|
|
});
|
|
|
|
app.get('/api/sensors', (req, res) => {
|
|
res.json(sensors);
|
|
});
|
|
|
|
app.get('/api/sensors/:id', (req, res) => {
|
|
const sensor = sensors.find(
|
|
(item) => item.id === req.params.id
|
|
);
|
|
|
|
if(!sensor) {
|
|
return res.status(404).json({
|
|
error: 'Sensor not found'
|
|
});
|
|
}
|
|
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');
|
|
});
|