Skip to content

Commit 6e4bf6a

Browse files
committed
[doc] Breakout demo.js into files in example/. Add web-socket-proxy.js example
1 parent b0b0183 commit 6e4bf6a

7 files changed

+914
-54
lines changed

examples/basic-proxy.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
demo.js: http proxy for node.js
3+
4+
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining
7+
a copy of this software and associated documentation files (the
8+
"Software"), to deal in the Software without restriction, including
9+
without limitation the rights to use, copy, modify, merge, publish,
10+
distribute, sublicense, and/or sell copies of the Software, and to
11+
permit persons to whom the Software is furnished to do so, subject to
12+
the following conditions:
13+
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
25+
*/
26+
27+
var util = require('util'),
28+
colors = require('colors')
29+
http = require('http'),
30+
httpProxy = require('./../lib/node-http-proxy');
31+
32+
//
33+
// Basic Http Proxy Server
34+
//
35+
httpProxy.createServer(9000, 'localhost').listen(8000);
36+
37+
//
38+
// Target Http Server
39+
//
40+
http.createServer(function (req, res) {
41+
res.writeHead(200, {'Content-Type': 'text/plain'});
42+
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
43+
res.end();
44+
}).listen(9000);
45+
46+
util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
47+
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow);

demo.js renamed to examples/forward-proxy.js

+6-54
Original file line numberDiff line numberDiff line change
@@ -27,67 +27,17 @@
2727
var util = require('util'),
2828
colors = require('colors')
2929
http = require('http'),
30-
httpProxy = require('./lib/node-http-proxy');
31-
32-
// ascii art from http://github.com/marak/asciimo
33-
var welcome = '\
34-
# # ##### ##### ##### ##### ##### #### # # # # \n\
35-
# # # # # # # # # # # # # # # # \n\
36-
###### # # # # ##### # # # # # # ## # \n\
37-
# # # # ##### ##### ##### # # ## # \n\
38-
# # # # # # # # # # # # # \n\
39-
# # # # # # # # #### # # # \n';
40-
util.puts(welcome.rainbow.bold);
41-
42-
//
43-
// Basic Http Proxy Server
44-
//
45-
httpProxy.createServer(9000, 'localhost').listen(8000);
46-
util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
47-
48-
//
49-
// Http Proxy Server with Proxy Table
50-
//
51-
httpProxy.createServer({
52-
router: {
53-
'localhost': 'localhost:9000'
54-
}
55-
}).listen(8001);
56-
util.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8001 '.yellow + 'with proxy table'.magenta.underline)
57-
58-
//
59-
// Http Proxy Server with Latency
60-
//
61-
httpProxy.createServer(function (req, res, proxy) {
62-
var buffer = proxy.buffer(req);
63-
setTimeout(function() {
64-
proxy.proxyRequest(req, res, 9000, 'localhost', buffer);
65-
}, 200)
66-
}).listen(8002);
67-
util.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8002 '.yellow + 'with latency'.magenta.underline);
68-
69-
//
30+
httpProxy = require('./../lib/node-http-proxy');
31+
7032
//
33+
// Setup proxy server with forwarding
7134
//
7235
httpProxy.createServer(9000, 'localhost', {
7336
forward: {
7437
port: 9001,
7538
host: 'localhost'
7639
}
7740
}).listen(8003);
78-
util.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8003 '.yellow + 'with forward proxy'.magenta.underline)
79-
80-
//
81-
// Http Server with proxyRequest Handler and Latency
82-
//
83-
var standAloneProxy = new httpProxy.HttpProxy();
84-
http.createServer(function (req, res) {
85-
var buffer = standAloneProxy.buffer(req);
86-
setTimeout(function() {
87-
proxy.proxyRequest(req, res, 9000, 'localhost', buffer);
88-
}, 200);
89-
}).listen(8004);
90-
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '8004 '.yellow + 'with proxyRequest handler'.cyan.underline + ' and latency'.magenta);
9141

9242
//
9343
// Target Http Server
@@ -97,7 +47,6 @@ http.createServer(function (req, res) {
9747
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
9848
res.end();
9949
}).listen(9000);
100-
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow);
10150

10251
//
10352
// Target Http Forwarding Server
@@ -108,4 +57,7 @@ http.createServer(function (req, res) {
10857
res.write('request successfully forwarded to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
10958
res.end();
11059
}).listen(9001);
60+
61+
util.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8003 '.yellow + 'with forward proxy'.magenta.underline)
62+
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow);
11163
util.puts('http forward server '.blue + 'started '.green.bold + 'on port '.blue + '9001 '.yellow);

examples/latent-proxy.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
demo.js: http proxy for node.js
3+
4+
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining
7+
a copy of this software and associated documentation files (the
8+
"Software"), to deal in the Software without restriction, including
9+
without limitation the rights to use, copy, modify, merge, publish,
10+
distribute, sublicense, and/or sell copies of the Software, and to
11+
permit persons to whom the Software is furnished to do so, subject to
12+
the following conditions:
13+
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
25+
*/
26+
27+
var util = require('util'),
28+
colors = require('colors')
29+
http = require('http'),
30+
httpProxy = require('./../lib/node-http-proxy');
31+
32+
//
33+
// Http Proxy Server with Latency
34+
//
35+
httpProxy.createServer(function (req, res, proxy) {
36+
var buffer = proxy.buffer(req);
37+
setTimeout(function() {
38+
proxy.proxyRequest(req, res, 9000, 'localhost', buffer);
39+
}, 200)
40+
}).listen(8002);
41+
42+
//
43+
// Target Http Server
44+
//
45+
http.createServer(function (req, res) {
46+
res.writeHead(200, {'Content-Type': 'text/plain'});
47+
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
48+
res.end();
49+
}).listen(9000);
50+
51+
util.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8002 '.yellow + 'with latency'.magenta.underline);
52+
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow);

examples/proxy-table.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
demo.js: http proxy for node.js
3+
4+
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining
7+
a copy of this software and associated documentation files (the
8+
"Software"), to deal in the Software without restriction, including
9+
without limitation the rights to use, copy, modify, merge, publish,
10+
distribute, sublicense, and/or sell copies of the Software, and to
11+
permit persons to whom the Software is furnished to do so, subject to
12+
the following conditions:
13+
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
25+
*/
26+
27+
var util = require('util'),
28+
colors = require('colors')
29+
http = require('http'),
30+
httpProxy = require('./../lib/node-http-proxy');
31+
32+
//
33+
// Http Proxy Server with Proxy Table
34+
//
35+
httpProxy.createServer({
36+
router: {
37+
'localhost': 'localhost:9000'
38+
}
39+
}).listen(8001);
40+
41+
//
42+
// Target Http Server
43+
//
44+
http.createServer(function (req, res) {
45+
res.writeHead(200, {'Content-Type': 'text/plain'});
46+
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
47+
res.end();
48+
}).listen(9000);
49+
50+
util.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8001 '.yellow + 'with proxy table'.magenta.underline)
51+
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow);

examples/standalone-proxy.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
demo.js: http proxy for node.js
3+
4+
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining
7+
a copy of this software and associated documentation files (the
8+
"Software"), to deal in the Software without restriction, including
9+
without limitation the rights to use, copy, modify, merge, publish,
10+
distribute, sublicense, and/or sell copies of the Software, and to
11+
permit persons to whom the Software is furnished to do so, subject to
12+
the following conditions:
13+
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
25+
*/
26+
27+
var util = require('util'),
28+
colors = require('colors')
29+
http = require('http'),
30+
httpProxy = require('./../lib/node-http-proxy');
31+
32+
//
33+
// Http Server with proxyRequest Handler and Latency
34+
//
35+
var proxy = new httpProxy.HttpProxy();
36+
http.createServer(function (req, res) {
37+
var buffer = proxy.buffer(req);
38+
setTimeout(function() {
39+
proxy.proxyRequest(req, res, 9000, 'localhost', buffer);
40+
}, 200);
41+
}).listen(8004);
42+
43+
//
44+
// Target Http Server
45+
//
46+
http.createServer(function (req, res) {
47+
res.writeHead(200, {'Content-Type': 'text/plain'});
48+
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
49+
res.end();
50+
}).listen(9000);
51+
52+
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '8004 '.yellow + 'with proxyRequest handler'.cyan.underline + ' and latency'.magenta);
53+
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow);

examples/web-socket-proxy.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
demo.js: http proxy for node.js
3+
4+
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining
7+
a copy of this software and associated documentation files (the
8+
"Software"), to deal in the Software without restriction, including
9+
without limitation the rights to use, copy, modify, merge, publish,
10+
distribute, sublicense, and/or sell copies of the Software, and to
11+
permit persons to whom the Software is furnished to do so, subject to
12+
the following conditions:
13+
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
25+
*/
26+
27+
var sys = require('sys'),
28+
http = require('http'),
29+
websocket = require('./websocket'),
30+
utils = require('socket.io/lib/socket.io/utils'),
31+
io = require('socket.io'),
32+
httpProxy = require('./../lib/node-http-proxy');
33+
34+
//
35+
// Create the target HTTP server
36+
//
37+
var server = http.createServer(function (req, res) {
38+
res.writeHead(200);
39+
res.end();
40+
});
41+
server.listen(8080);
42+
43+
//
44+
// Setup socket.io on the target HTTP server
45+
//
46+
var socket = io.listen(server);
47+
socket.on('connection', function (client) {
48+
sys.debug('Got websocket connection');
49+
50+
client.on('message', function (msg) {
51+
sys.debug('Got message from client: ' + msg);
52+
});
53+
54+
socket.broadcast('from server');
55+
});
56+
57+
//
58+
// Create a proxy server with node-http-proxy
59+
//
60+
var proxy = httpProxy.createServer(8080, 'localhost');
61+
proxy.listen(8081);
62+
63+
//
64+
// Setup the web socket against our proxy
65+
//
66+
var ws = new websocket.WebSocket('ws://localhost:8081/socket.io/websocket/', 'borf');
67+
68+
ws.on('open', function () {
69+
ws.send(utils.encode('from client'));
70+
});
71+
72+
ws.on('message', function (msg) {
73+
sys.debug('Got message: ' + utils.decode(msg));
74+
});

0 commit comments

Comments
 (0)