diff --git a/index.js b/index.js index 63fc8b7..3ef1154 100644 --- a/index.js +++ b/index.js @@ -69,6 +69,21 @@ app.get('/api/measurements', (req, res) => { res.json(measurements); }); +app.get('/api/measurements/latest', (req, res) => { + // Check whether any measurements exist. + if (measurements.length === 0) { + return res.status(404).json({ + error: 'No measurements yet' + }); + } + + // The last array element is the latest submitted measurement. + const latest = measurements[measurements.length - 1]; + + res.json(latest); +}); + + // starting the listening service app.listen(3030, () => { console.log('Server running at http://localhost:3030');