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.
80 lines
1.4 KiB
JavaScript
80 lines
1.4 KiB
JavaScript
const express = require('express');
|
|
const app = express();
|
|
|
|
app.use(express.json());
|
|
|
|
const measurements = [];
|
|
|
|
const sensors = [
|
|
{
|
|
id: 'htu21d-01',
|
|
model: 'HTU21D',
|
|
quantities: ['temperature', 'humidity']
|
|
},
|
|
{
|
|
id: 'htu21d-02',
|
|
model: 'HTU21D',
|
|
quantities: ['temperature', 'humidity']
|
|
}
|
|
];
|
|
|
|
app.get('/', (req, res) => {
|
|
res.send('<h1> TEST </h1>');
|
|
});
|
|
|
|
//Status//
|
|
app.get('/api/status', (req, res) => {
|
|
res.json({ status: 'online', application: 'sensor-api' });
|
|
});
|
|
|
|
app.get('/api/info', (req, res) => {
|
|
res.json({ author: 'Leonard', version: 'v0.0' });
|
|
});
|
|
|
|
//SENSORS//
|
|
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 not found
|
|
if (!sensor) {
|
|
return res.status(404).json({
|
|
error: 'sensor not found',
|
|
});
|
|
}
|
|
res.json(sensor);
|
|
});
|
|
|
|
app.post('/api/measurements', (req, res) => {
|
|
//require json
|
|
//receive measurements
|
|
const measurement = req.body;
|
|
//verify type
|
|
// Content-type application/json
|
|
|
|
//validate submitted fields
|
|
|
|
|
|
//store in memory
|
|
measurements.push(measurement);
|
|
|
|
//show in terminal
|
|
console.log('Received:', measurement);
|
|
|
|
//confirm status and storing
|
|
res.status(201).json(measurement);
|
|
});
|
|
|
|
app.get('/api/measurements', (req, res) => {
|
|
res.json(measurements);
|
|
});
|
|
|
|
app.listen(3030, () => {
|
|
console.log('Listen on port 3030');
|
|
});
|