-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_server.js
More file actions
58 lines (44 loc) · 1.1 KB
/
Copy pathnode_server.js
File metadata and controls
58 lines (44 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var http = require('http');
var express = require("express");
var app = express();
var body = '';
var options = {
host: 'dashboard.glasgow.gov.uk',
port: 80,
path: '/api/live/parking.php?type=json'
};
function pull() {
http.get(options, function(res) {
console.log('started pull');
var previous = body;
body = '';
res.on('data', function(chunk) {
if (chunk != '{"error":"RATE_LIMIT"}'){
body += chunk;
} else {
body = previous;
console.log('using cache');
}
});
res.on("end", function() {
console.log("BODY: " + body);
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
}
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.use(express.static(__dirname + '/app'));
app.get('/api', function (req, res) {
pull();
res.send(body);
});
app.get('/', function(req, res){
res.sendfile(__dirname + '/index.html');
});
var port = Number(process.env.PORT || 4242);
app.listen(port);