From 4ad66ed256458033d9dfa9c4cd3b9f295366e65c Mon Sep 17 00:00:00 2001 From: Pieter Colpaert Date: Sun, 2 Aug 2015 15:37:41 +0200 Subject: [PATCH 1/2] enabled cors, added config --- .gitignore | 3 +++ config-example.json | 7 +++++++ package.json | 2 +- public/js/script.js | 12 +++++------- script.js | 34 +++++++++++++++------------------- 5 files changed, 31 insertions(+), 27 deletions(-) create mode 100644 config-example.json 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/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 0023e1f..859577c 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 2f6aea4..113c333 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'); 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); }}); @@ -102,8 +102,6 @@ console.log('Hi there'); ""+ ""); // console.log(" i am an awesome cat " + result.entity[i].alert.header_text.translation[0].text); - - } } -}); \ No newline at end of file +}); diff --git a/script.js b/script.js index 1891569..294537b 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,7 +79,7 @@ setInterval(function() { //console.log('Hello World > helloworld.txt'); }); //console.log(feed.entity[0]); - + } }); @@ -134,7 +130,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; From 3f60452408f72c95683f1c0da3e2593e210091dd Mon Sep 17 00:00:00 2001 From: Pieter Colpaert Date: Sun, 2 Aug 2015 15:47:06 +0200 Subject: [PATCH 2/2] Update README --- README.md | 11 +++++++++-- script.js | 3 --- 2 files changed, 9 insertions(+), 5 deletions(-) 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/script.js b/script.js index 95f6624..619b4fd 100644 --- a/script.js +++ b/script.js @@ -82,9 +82,6 @@ setInterval(function() { } }); - - - }, the_interval); app.get('/servicesAlerts', function (req, res) {