-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (40 loc) · 1.04 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
var connect = require('connect');
var request = require('request');
var url = require('url');
var app = connect();
function log() {
return;
console.log.apply(console, arguments);
}
function error() {
return;
console.error.apply(console, arguments);
}
app.use(function (req, downstreamRes) {
var query = url.parse(req.url, true).query;
var upstreamUrl = query.url;
var successCallback = query.callback;
if (!upstreamUrl) {
return;
}
log('requesting ' + upstreamUrl);
request.get(upstreamUrl, function (error, upstreamRes, script) {
if (error) {
error("Error:", upstreamUrl, error)
return;
}
log('done ' + upstreamUrl);
if (upstreamRes.status >= 400) {
downstreamRes.write(successCallback + '("');
downstreamRes.write(JSON.stringify({error: script}));
downstreamRes.write(');');
downstreamRes.end();
return;
}
downstreamRes.write(successCallback + '(');
downstreamRes.write(JSON.stringify({success: script}));
downstreamRes.write(');');
downstreamRes.end();
});
});
app.listen(process.env.PORT || 8000);