forked from lfarcoso/knowyourtunes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstackmobserver.js
67 lines (50 loc) · 1.63 KB
/
stackmobserver.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
66
67
var http = require('http')
, httpStatic = require('node-static')
, file = new httpStatic.Server('.')
, port = process.env.PORT || 4567
, STACKMOB_API_SERVER = 'api.stackmob.com';
http.createServer(function(request, response) {
if(request.headers['x-stackmob-proxy-plain']) {
var host = request.headers['host'];
if(host.toString().indexOf(':') != -1) {
host = host.split(':');
} else {
host = [host, 80];
}
var headers = request.headers;
headers['host'] = STACKMOB_API_SERVER,
headers['x-forwarded-for'] = host[0],
headers['x-stackmob-forwarded-port'] = host[1],
headers['x-stackmob-forwared-host'] = host[0],
headers['x-forwarded-proto'] = 'HTTP',
headers['version'] = 'HTTP/1.1'
var options = {
host: STACKMOB_API_SERVER,
port: 80,
method: request.method,
path: request.url,
headers: headers
};
var proxyRequest = http.request(options, function(proxyResponse) {
proxyResponse.on('data', function(chunk) {
response.write(chunk, 'binary');
});
proxyResponse.on('end', function() {
response.end();
});
response.writeHead(proxyResponse.statusCode, proxyResponse.headers);
});
request.on('data', function(chunk) {
proxyRequest.write(chunk, 'binary');
});
request.on('end', function() {
proxyRequest.end();
});
proxyRequest.on('error', function(error) {
console.log(error);
});
} else {
file.serve(request, response);
}
}).listen(port);
console.log("StackMob listening at " + port);