Working API

This commit is contained in:
Lukas Bachschwell 2017-09-18 18:45:21 +02:00
parent 9b8f5f77a8
commit 45f24fd31c

42
index.js Normal file
View File

@ -0,0 +1,42 @@
// 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
// Import required modules.
let express = require('express');
let fs = require('fs');
let bikeData = JSON.parse(fs.readFileSync('data.json', 'utf8'));
// Initialize our Express app.
let app = express();
const filterStations = (stations, stationId) => {
let returnObject;
stations.map((o,index)=>{
if(o.extra.uid == stationId){
returnObject = o;
}
});
return returnObject;
};
// Generate a simple API
app.get('/', function(req, res) {
res.send("Hey there! Thanks for visting the site!");
});
// Generate a simple dashboard page.
app.get('/stations', (req, res) => {
if(req.param('stationId')){
let returnValue = {status:"not found"};
returnValue = filterStations(bikeData.network.stations, req.param('stationId'));
res.json(returnValue);
}else{
res.json({status:"error"});
}
});
// Listen for incoming requests and serve them.
app.listen(process.env.PORT || 3000);