diff --git a/.gitignore b/.gitignore index 5e80f8a..9d069a3 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,6 @@ build/Release # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git node_modules .idea/ + +# config file for deployment +config.json \ No newline at end of file diff --git a/README.md b/README.md index cb141f3..7f15f1d 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,15 @@ Show the real-time data of the Belgian railway company in a simple overview -## install - +After cloning this repo, perform ```bash npm install ``` + +Before running the server, make sure your config.json is set. See the config-example.json for an example. + +Run the server using + +```bash +nodejs script.js +``` diff --git a/config-example.json b/config-example.json new file mode 100644 index 0000000..4ee0dc2 --- /dev/null +++ b/config-example.json @@ -0,0 +1,7 @@ +{ + "port" : "3000", + "gtfs-urls" : { + "service-alerts" : "http://irail.gent/service_alerts.pb", + "trip-updates" : "http://irail.gent/trip_updates.pb" + } +} diff --git a/package.json b/package.json index 286267a..6657069 100644 --- a/package.json +++ b/package.json @@ -22,8 +22,8 @@ "url": "https://github.com/oSoc15/gtfs-rt-demo/issues" }, "dependencies": { + "cors": "^2.7.1", "express": "^4.13.1", - "fs": "0.0.2", "gtfs-realtime-bindings": "0.0.4", "protocol-buffers": "^3.1.2", "request": "^2.60.0" diff --git a/public/js/script.js b/public/js/script.js index 98fdce0..dba8fb1 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -6,12 +6,12 @@ $(document).ready(function(){ console.log('Hi there'); - $.ajax({url: "http://localhost:3000/service_alerts.json", success: function(result){ + $.ajax({url: "/service_alerts.json", success: function(result){ console.log('Hi data'); console.log(result.header); parseServiceAlertData(result); }}); - $.ajax({url: "http://localhost:3000/trip_updates.json", success: function(result){ + $.ajax({url: "/trip_updates.json", success: function(result){ console.log('Hi data'); console.log(result.header); parseTripUpdateData(result); @@ -20,10 +20,10 @@ console.log('Hi there'); window.setInterval(function() { console.log('Hi Minute'); - $.ajax({url: "http://localhost:3000/service_alerts.json", success: function(result){ + $.ajax({url: "/service_alerts.json", success: function(result){ parseServiceAlertData(result); }}); - $.ajax({url: "http://localhost:3000/trip_updates.json", success: function(result){ + $.ajax({url: "/trip_updates.json", success: function(result){ parseTripUpdateData(result); }}); @@ -108,9 +108,6 @@ console.log('Hi there'); totalDelay+ ""+ ""); - - - } } -}); \ No newline at end of file +}); diff --git a/script.js b/script.js index 1891569..619b4fd 100644 --- a/script.js +++ b/script.js @@ -1,25 +1,26 @@ -/** - * Created by timtijssens on 29/07/15. - */ +var GtfsRealtimeBindings = require('gtfs-realtime-bindings'), + request = require('request'), + express = require('express'), + cors = require('cors'), + fs = require('fs'); - -var GtfsRealtimeBindings = require('gtfs-realtime-bindings'); -var request = require('request'); -var express = require('express'); var app = express(); app.use('/', express.static('public')); -fs = require('fs'); +app.use('/service_alerts.json',cors()); +app.use('/trip_updates.json',cors()); + +var config = JSON.parse(fs.readFileSync('config.json', 'utf8')); var requestSettingsTripUpdates = { method: 'GET', //url: 'http://localhost:8000/trip_updates.pb', - url: 'http://irail.gent/trip_updates.pb', + url: config['gtfs-urls']['trip-updates'], encoding: null }; var requestSettingsServiceAlerts = { method: 'GET', //url: 'http://localhost:8000/service_alerts.pb', - url: 'http://irail.gent/service_alerts.pb', + url: config['gtfs-urls']['service-alerts'], encoding: null }; @@ -31,26 +32,21 @@ request(requestSettingsServiceAlerts, function (error, response, body) { fs.writeFile('public/service_alerts.json', JSON.stringify(feed), function (err) { if (err) return console.log(err); - }); - - } }); -request(requestSettingsTripUpdates, function (error, response, body) { +request(requestSettingsTripUpdates, function (error, response, body) { if (!error && response.statusCode == 200) { - var feed = GtfsRealtimeBindings.FeedMessage.decode(body); - fs.writeFile('public/trip_updates.json', JSON.stringify(feed), function (err) { if (err) return console.log(err); //console.log('Hello World > helloworld.txt'); }); //console.log(feed.entity[0]); - } }); + var minutes = 1, the_interval = minutes * 60 * 1000; setInterval(function() { console.log("I am doing my minute check"); @@ -83,18 +79,11 @@ setInterval(function() { //console.log('Hello World > helloworld.txt'); }); //console.log(feed.entity[0]); - + } }); - - - }, the_interval); - - - - app.get('/servicesAlerts', function (req, res) { console.log("Got Service Alerts Request"); @@ -134,7 +123,7 @@ app.get('/tripUpdates', function (req, res) { }); }); -var server = app.listen(3000, function () { +var server = app.listen(config.port, function () { var host = server.address().address; var port = server.address().port;