-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.js
More file actions
46 lines (36 loc) · 949 Bytes
/
Copy pathproxy.js
File metadata and controls
46 lines (36 loc) · 949 Bytes
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
var http = require('http');
http.createServer(function(request, response) {
var options = {
hostname: 'www.youtube.com',
port: 80,
path: request.url,
method: request.method
}
// do proxy request
var proxy = http.request(options, function (res) {
// response back from proxy request
res.setEncoding('utf8');
res.on('data', function (chunk) {
// output to browser
response.write(chunk, 'binary');
});
res.on('end', function() {
// end output to browser
response.end();
proxy.end();
});
response.writeHead(res.statusCode, res.headers);
});
// handle error
proxy.on('error', function(e) {
reponse.write("problem with request: " + e.message);
reponse.end();
});
// write data to request body
request.on('data', function(chunk) {
proxy.write(chunk, 'binary');
});
request.on('end', function() {
proxy.end();
});
}).listen(8000);