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.
84 lines
1.6 KiB
JavaScript
84 lines
1.6 KiB
JavaScript
const express = require("express");
|
|
|
|
const app = express();
|
|
const PORT = 3030;
|
|
app.use(express.json());
|
|
const measurements = [ ]; //empty array
|
|
const sensors = [
|
|
{
|
|
id: 'htu21d-01',
|
|
model: 'HTU21D',
|
|
quantities: ['temperature', 'humidity'],
|
|
},
|
|
|
|
{
|
|
id: 'htu21d-02',
|
|
model: 'HTU21D',
|
|
quantities: ['temperature', 'humidity'],
|
|
}
|
|
]
|
|
|
|
app.use(express.json());
|
|
|
|
app.get("/", (req, res) => {
|
|
res.send("Sensor Logger API is running");
|
|
});
|
|
//status
|
|
app.get('/sensor_logger_API/status',(req, res) => {
|
|
res.json({ status: "online", application: 'sensor-api'});
|
|
});
|
|
|
|
|
|
app.get('/sensor_logger_API/info',(req, res) => {
|
|
res.json({ version: "v1.0"});
|
|
});
|
|
|
|
|
|
app.get('/sensor_logger_API/sensors',(req, res) => {
|
|
res.json(sensors);
|
|
});
|
|
|
|
app.get('/sensor_logger_API/sensors/:id',(req, res) => {
|
|
const sensor = sensor.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('/sensor_logger_API/measurements', (req, res) => {
|
|
//require json structure
|
|
|
|
//recieve the measurement
|
|
const measurement = req.body;
|
|
//verify typo of object
|
|
|
|
//vcalidate submitted fields id
|
|
|
|
//verify type of object
|
|
|
|
//validate submitted fields
|
|
|
|
//store in memory}
|
|
measurements.push(measurement);
|
|
//show it in terminal
|
|
console.log('Recieved:',measurement);
|
|
//confirm status and storing
|
|
res.status(201).json(measurement);
|
|
}
|
|
|
|
);
|
|
|
|
app.get('/sensor_logger_API/measurements', (req, res) => {
|
|
res.json(measurements);
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`Server listening on port ${PORT}`);
|
|
});
|