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.
52 lines
923 B
JavaScript
52 lines
923 B
JavaScript
const express = require('express');
|
|
const app = express();
|
|
|
|
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.listen(3030, () => {
|
|
console.log('Listen on port 3030');
|
|
});
|