Skip to content

Commit 16828a9

Browse files
committed
Merge pull request #520 from nodejitsu/better-examples
Better examples
2 parents cfd417d + e2a5d51 commit 16828a9

30 files changed

+1438
-150
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ notifications:
99
irc: "irc.freenode.org#nodejitsu"
1010

1111
script:
12-
npm run-script coveralls
12+
npm test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
simple-balancer.js: Example of a simple round robin balancer for websockets
3+
4+
Copyright (c) Nodejitsu 2013
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 http = require('http'),
28+
httpProxy = require('../../lib/http-proxy');
29+
30+
//
31+
// A simple round-robin load balancing strategy.
32+
//
33+
// First, list the servers you want to use in your rotation.
34+
//
35+
var addresses = [
36+
{
37+
host: 'ws1.0.0.0',
38+
port: 80
39+
},
40+
{
41+
host: 'ws2.0.0.0',
42+
port: 80
43+
}
44+
];
45+
46+
//
47+
// Create a HttpProxy object for each target
48+
//
49+
50+
var proxies = addresses.map(function (target) {
51+
return new httpProxy.createProxyServer({
52+
target: target
53+
});
54+
});
55+
56+
//
57+
// Get the proxy at the front of the array, put it at the end and return it
58+
// If you want a fancier balancer, put your code here
59+
//
60+
61+
function nextProxy() {
62+
var proxy = proxies.shift();
63+
proxies.push(proxy);
64+
return proxy;
65+
}
66+
67+
//
68+
// Get the 'next' proxy and send the http request
69+
//
70+
71+
var server = http.createServer(function (req, res) {
72+
nextProxy().web(req, res);
73+
});
74+
75+
//
76+
// Get the 'next' proxy and send the upgrade request
77+
//
78+
79+
server.on('upgrade', function (req, socket, head) {
80+
nextProxy().ws(req, socket, head);
81+
});
82+
83+
server.listen(8001);
84+

examples/balancer/simple-balancer.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
simple-balancer.js: Example of a simple round robin balancer
3+
4+
Copyright (c) Nodejitsu 2013
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 http = require('http'),
28+
httpProxy = require('../../lib/http-proxy');
29+
//
30+
// A simple round-robin load balancing strategy.
31+
//
32+
// First, list the servers you want to use in your rotation.
33+
//
34+
var addresses = [
35+
{
36+
host: 'ws1.0.0.0',
37+
port: 80
38+
},
39+
{
40+
host: 'ws2.0.0.0',
41+
port: 80
42+
}
43+
];
44+
var proxy = httpProxy.createServer();
45+
46+
http.createServer(function (req, res) {
47+
//
48+
// On each request, get the first location from the list...
49+
//
50+
var target = { target: addresses.shift() };
51+
52+
//
53+
// ...then proxy to the server whose 'turn' it is...
54+
//
55+
console.log('balancing request to: ', target);
56+
proxy.web(req, res, target);
57+
58+
//
59+
// ...and then the server you just used becomes the last item in the list.
60+
//
61+
addresses.push(target);
62+
}).listen(8021);
63+
64+
// Rinse; repeat; enjoy.

examples/concurrent-proxy.js

-36
This file was deleted.

examples/error-handling.js

-33
This file was deleted.

examples/forward-proxy.js

-33
This file was deleted.

examples/helpers/store.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
//
3+
// just to make these example a little bit interesting,
4+
// make a little key value store with an http interface
5+
// (see couchbd for a grown-up version of this)
6+
//
7+
// API:
8+
// GET /
9+
// retrive list of keys
10+
//
11+
// GET /[url]
12+
// retrive object stored at [url]
13+
// will respond with 404 if there is nothing stored at [url]
14+
//
15+
// POST /[url]
16+
//
17+
// JSON.parse the body and store it under [url]
18+
// will respond 400 (bad request) if body is not valid json.
19+
//
20+
// TODO: cached map-reduce views and auto-magic sharding.
21+
//
22+
var Store = module.exports = function Store () {
23+
this.store = {};
24+
};
25+
26+
Store.prototype = {
27+
get: function (key) {
28+
return this.store[key]
29+
},
30+
set: function (key, value) {
31+
return this.store[key] = value
32+
},
33+
handler:function () {
34+
var store = this
35+
return function (req, res) {
36+
function send (obj, status) {
37+
res.writeHead(200 || status,{'Content-Type': 'application/json'})
38+
res.write(JSON.stringify(obj) + '\n')
39+
res.end()
40+
}
41+
var url = req.url.split('?').shift()
42+
if (url === '/') {
43+
console.log('get index')
44+
return send(Object.keys(store.store))
45+
} else if (req.method == 'GET') {
46+
var obj = store.get (url)
47+
send(obj || {error: 'not_found', url: url}, obj ? 200 : 404)
48+
} else {
49+
//post: buffer body, and parse.
50+
var body = '', obj
51+
req.on('data', function (c) { body += c})
52+
req.on('end', function (c) {
53+
try {
54+
obj = JSON.parse(body)
55+
} catch (err) {
56+
return send (err, 400)
57+
}
58+
store.set(url, obj)
59+
send({ok: true})
60+
})
61+
}
62+
}
63+
}
64+
}

examples/http/basic-proxy.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
basic-proxy.js: Basic example of proxying over HTTP
3+
4+
Copyright (c) Nodejitsu 2013
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/http-proxy');
31+
32+
var welcome = [
33+
'# # ##### ##### ##### ##### ##### #### # # # #',
34+
'# # # # # # # # # # # # # # # # ',
35+
'###### # # # # ##### # # # # # # ## # ',
36+
'# # # # ##### ##### ##### # # ## # ',
37+
'# # # # # # # # # # # # # ',
38+
'# # # # # # # # #### # # # '
39+
].join('\n');
40+
41+
util.puts(welcome.rainbow.bold);
42+
43+
//
44+
// Basic Http Proxy Server
45+
//
46+
httpProxy.createServer({
47+
target:'http://localhost:9003'
48+
}).listen(8003);
49+
50+
//
51+
// Target Http Server
52+
//
53+
http.createServer(function (req, res) {
54+
res.writeHead(200, { 'Content-Type': 'text/plain' });
55+
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
56+
res.end();
57+
}).listen(9003);
58+
59+
util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8003'.yellow);
60+
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9003 '.yellow);

0 commit comments

Comments
 (0)