From 60a9c29c9a4c535faf1f3f67a13ce070a49675b5 Mon Sep 17 00:00:00 2001 From: Lukas Bachschwell Date: Tue, 19 Sep 2017 00:06:11 +0200 Subject: [PATCH] Working version --- index.js | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 3ecdb87..12c97ff 100644 --- a/index.js +++ b/index.js @@ -1,16 +1,12 @@ -// Request all stuff from citybikes.es -//save to json all 500 ms -//https://api.citybik.es/v2/networks/valenbisi?fields=stations - -//Setup a express get server with token - +'use strict'; // Import required modules. let express = require('express'); -let fs = require('fs'); -let bikeData = JSON.parse(fs.readFileSync('data.json', 'utf8')); +let axios = require('axios'); +let bikeData = {}; // Initialize our Express app. let app = express(); +let requestDataTimer = setInterval(requestData, 3000); const filterStations = (stations, stationId) => { let returnObject; @@ -29,14 +25,25 @@ app.get('/', function(req, res) { // Generate a simple dashboard page. app.get('/stations', (req, res) => { - if(req.param('stationId')){ + if(req.query.stationId){ let returnValue = {status:"not found"}; - returnValue = filterStations(bikeData.network.stations, req.param('stationId')); + returnValue = filterStations(bikeData.network.stations, req.query.stationId); res.json(returnValue); }else{ res.json({status:"error"}); } }); +function requestData(){ + + axios.get('https://api.citybik.es/v2/networks/valenbisi',{headers: {'Accept-Encoding': 'gzip, deflate, br'}}) + .then(function (response) { + //console.log(response); + bikeData = response.data; + }) + .catch(function (error) { + console.log(error); + }); +} // Listen for incoming requests and serve them. app.listen(process.env.PORT || 3000);