-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
78 lines (70 loc) · 1.76 KB
/
app.js
File metadata and controls
78 lines (70 loc) · 1.76 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
var express = require('express');
var http = require('http');
var https = require('https');
var app = express();
app.use(express.static('public'));
var dirty = false;
var eggData = '';
function setDirty(){
dirty = true;
}
app.get('/eggs', function(req, res){
if(!eggData || dirty){
var tempEggData = '';
var req = https.request({
hostname:"api.xively.com",
path:'/v2/feeds.json?amp;mapped=true&tag=device:type=airqualityegg&per_page=50&lat=41.696116260522786&lon=44.812652967284656&distance=600',
method:'GET',
headers:{
'Content-Type':'application/json',
'X-ApiKey':'yy7Q62aWBIo9rwHhO4DAG0L5SLD2cms8bUnlJ07xnOpr0jdl'
}
}, function(res2){
if(res2.statusCode !== 200){
res.send(eggData);
}else{
res2.on('data', function(chunk){
tempEggData+=chunk.toString();
});
res2.on('end', function(){
if(tempEggData){
eggData = tempEggData;
}
console.log(eggData.substring(0, 100));
res.send(eggData);
});
}
});
req.end();
dirty = false;
//cache expires half-hourly
setTimeout(setDirty, 1000*30);
}else{
res.send(eggData);
}
})
app.get('/eggPage/:page', function(req, res){
var tempEggData='';
var req = https.request({
hostname:"api.xively.com",
path:'/v2/feeds.json?amp;mapped=true&tag=device:type=airqualityegg&per_page=300&page='+req.params.page,
method:'GET',
headers:{
'Content-Type':'application/json',
'X-ApiKey':'yy7Q62aWBIo9rwHhO4DAG0L5SLD2cms8bUnlJ07xnOpr0jdl'
}
}, function(res2){
res2.on('data', function(chunk){
tempEggData+=chunk.toString();
});
res2.on('end', function(){
if(tempEggData){
eggData = tempEggData;
}
res.send(tempEggData);
});
});
req.end();
});
console.log('Listening on port 3020');
http.createServer(app).listen(3020);