-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (51 loc) · 1.67 KB
/
index.js
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
var AUTHOR = 'Author: Cristian Dinu, Read Forward';
var LICENSE = 'License: MPL v2';
var express = require('express');
var app = express();
var compression = require('compression')
var request = require('request');
var fbJSON = JSON.stringify({});
var lastRefresh = 0;
var interval = process.env.REFRESH_INTERVAL || 120;
var pageId = process.env.FB_PAGE_ID || '130380413723811';
var userAgent = process.env.USER_AGENT ||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) ' +
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36';
app.use(compression());
app.get('/', function(req, res) {
res.type('text/plain');
res.end(['This is a web service, not a web page.',
AUTHOR,
LICENSE].join('\n'));
});
app.get('/v1.0/macovei-fb-proxy.json', function(req, res){
res.type('application/json');
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
res.end(fbJSON);
var now = +new Date();
if (now - lastRefresh > interval * 1000) {
refreshJSON();
}
});
var options = {
url: 'https://www.facebook.com/feeds/page.php?format=json&id=' + pageId,
headers: {
'User-Agent': userAgent
}
};
function refreshJSON () {
lastRefresh = +new Date();
request(options, function (error, response, body) {
if (!error && response.statusCode === 200) {
fbJSON = body;
} else {
console.error('Cannot fetch JSON', error);
fbJSON = JSON.stringify({});
}
});
}
refreshJSON();
var server = app.listen(process.env.PORT || 8080, function() {
console.log('Listening on port %d', server.address().port);
});