-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
120 lines (103 loc) · 3.97 KB
/
Copy pathserver.js
File metadata and controls
120 lines (103 loc) · 3.97 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var HOST = process.env.npm_package_config_hostname;
var PORT = process.env.npm_package_config_port;
var DEFAULT_REMOTE_HOST = process.env.npm_package_config_default_remote_hostname;
var DEFAULT_API_KEY = process.env.npm_package_config_default_api_key;
if (typeof DEFAULT_API_KEY === 'undefined') {
console.log('DEFAULT API KEY was undefined please define with "npm config set roxy:default_api_key 0123456789abcdef0123456789abcdef"');
process.kill(1);
}
var http = require("http");
var https = require("https");
var url = require("url");
var qs = require("qs");
var restService = function(request) {
return new function() {
this.remoteUrl = url.parse(request.url) || DEFAULT_REMOTE_HOST;
this.protocol = 'https:';
this.port = 443;
if (this.remoteUrl.protocol == 'http:') {
this.protocol = this.remoteUrl.protocol;
this.port = 80;
}
if (typeof this.remoteUrl.port != 'undefined') {
this.port = this.remoteUrl.port;
}
this.options = {
'hostname' : this.remoteUrl.hostname,
'port' : this.port,
'encoding' : 'utf8',
'method' : request.method,
'path' : '/',
'headers' : {
'Accept' : 'application/json',
'Authorization' : 'OAuth '+DEFAULT_API_KEY
}
};
for (var property in request.headers) {
this.options.headers[property] = request.headers[property];
}
this.request = function(path, success, error) {
var options = this.options;
options.path = this.remoteUrl.path+path;
//console.log(options);
var lhRequest;
if (this.protocol === 'http:') {
lhRequest = http.request(options, success);
}
else {
lhRequest = https.request(options, success);
}
lhRequest.on('error', error);
return lhRequest;
}
}
}
var server = http.createServer(function(req, res) {
var roxyResponse = res;
var params = qs.parse(url.parse(req.url, true).search.substr(1));
//console.log(params);
var request = params._request;
request.method = typeof request.method == 'undefined' ? 'GET' : request.method.toUpperCase();
var path = url.parse(req.url).pathname.substr(2);
var callback = params.callback;
delete params._;
delete params.callback;
delete params._request;
var remoteRequest = restService(request).request(
path,
function (lhResponse) {
lhResponse.responseString = '';
//console.log('STATUS: ' + lhResponse.statusCode);
lhResponse.setEncoding('utf8');
lhResponse.on('data', function (chunk) {
this.responseString += chunk;
});
lhResponse.on('end', function () {
roxyResponse.setHeader('Content-Type', 'text/javascript');
roxyResponse.write(callback+'('+this.responseString+');');
roxyResponse.end();
//console.log(roxyResponse);
//console.log('END: '+this.responseString);
});
lhResponse.on('close', function () {
roxyResponse.close();
console.log('CLOSE');
});
},
function (lhError) {
console.log('problem with request: ' + lhError.message);
roxyResponse.writeHead(500);
roxyResponse.end(lhError.message);
}
);
var lhParamString = ''
if (request.method != 'GET') {
//console.log(request.method);
lhParamString = qs.stringify(params);
//console.log(lhParamString);
remoteRequest.setHeader('Content-Length', lhParamString.length);
}
remoteRequest.write(lhParamString);
remoteRequest.end();
});
server.listen(PORT, HOST);