-
Notifications
You must be signed in to change notification settings - Fork 0
/
keep-alive-proxy.js
52 lines (45 loc) · 1.21 KB
/
keep-alive-proxy.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
/*
* Before execution: `npm install agentkeepalive`
*/
var http = require('http'),
url = require('url'),
Agent = require('agentkeepalive');
var keepaliveAgent = new Agent({
maxSockets: 5,
maxFreeSockets: 5,
keepAlive: true,
keepAliveMsecs: 30000
});
server = http.createServer(onRequest);
server.listen(4444);
console.log("Server started on port 4444");
function onRequest(client_req, client_res) {
console.log('serve:', client_req.url);
var parseURL = url.parse(client_req.url, true);
var req_headers = client_req.headers;
req_headers['connection'] = 'keep-alive';
var options = {
path: parseURL.path,
hostname: 'hub.browserstack.com',
port: 80,
method: client_req.method,
headers: req_headers,
agent: keepaliveAgent
};
var proxy = http.request(options, function (res) {
client_res.writeHead(res.statusCode, res.headers);
res.pipe(client_res);
delete options, req_headers;
}).on('error', function(e) {
console.log('req error:', e);
client_res.writeHead(500);
client_res.write('error: ' + e.toString());
client_res.end();
});
client_req.pipe(proxy, {
end: true
});
}
server.on('error', function(e) {
console.log('error', e);
});