Skip to content

Commit d490b50

Browse files
committed
added colors and asciimo
1 parent d6a2f8a commit d490b50

File tree

5 files changed

+209
-2
lines changed

5 files changed

+209
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@
77
###features
88

99
- reverse-proxies incoming http.Server requests
10+
- can be used as a CommonJS module in node.js
1011
- can handled malformed http requests
1112
- uses event buffering to support application latency in proxied requests
1213
- minimal request overhead and latency
1314
- fully-tested
1415
- battled-hardened through production usage @ nodejitsu.com
15-
16+
- written entirely in javascript
17+
- easy to use api
1618

1719
###when to use node-http-proxy
1820

1921
let's suppose you were running multiple http application servers, but you only wanted to expose one machine to the internet. you could setup node-http-proxy on that one machine and then reverse-proxy the incoming http requests to locally running services which were not exposed to the outside network.
2022

2123
### how to use node-http-proxy
22-

demo.js

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* node-proxy-test.js: Tests for node-proxy. Reverse proxy for node.js
3+
*
4+
* (C) 2010 Charlie Robbins
5+
* MIT LICENSE
6+
*
7+
*/
8+
9+
var vows = require('vows'),
10+
sys = require('sys'),
11+
colors = require('./vendor/colors')
12+
assert = require('assert'),
13+
http = require('http');
14+
15+
require.paths.unshift(require('path').join(__dirname, '../lib/'));
16+
17+
18+
sys.puts('node-http-proxy has started!'.rainbow);
19+
20+
21+
var NodeProxy = require('./lib/node-proxy').NodeProxy;
22+
var testServers = {};
23+
24+
//
25+
// Simple 'hello world' response for test purposes
26+
//
27+
var helloWorld = function(req, res) {
28+
res.writeHead(200, {'Content-Type': 'text/plain'});
29+
res.write('hello world')
30+
res.end();
31+
};
32+
33+
//
34+
// Creates the reverse proxy server
35+
//
36+
var startProxyServer = function (server, port, proxy) {
37+
var proxyServer = http.createServer(function (req, res){
38+
// Initialize the nodeProxy and start proxying the request
39+
proxy.init(req, res);
40+
proxy.proxyRequest(server, port, req, res);
41+
});
42+
43+
proxyServer.listen(8080);
44+
return proxyServer;
45+
};
46+
47+
//
48+
// Creates the reverse proxy server with a specified latency
49+
//
50+
var startLatentProxyServer = function (server, port, proxy, latency) {
51+
var proxyServer = http.createServer(function (req, res){
52+
// Initialize the nodeProxy and start proxying the request
53+
proxy.init(req, res);
54+
setTimeout(function () {
55+
proxy.proxyRequest(server, port, req, res);
56+
}, latency);
57+
});
58+
59+
proxyServer.listen(8081);
60+
return proxyServer;
61+
};
62+
63+
//
64+
// Creates the 'hellonode' server
65+
//
66+
var startTargetServer = function (port) {
67+
var targetServer = http.createServer(function (req, res) {
68+
helloWorld(req, res);
69+
})
70+
71+
targetServer.listen(port);
72+
return targetServer;
73+
};
74+
75+
//
76+
// The default test bootstrapper with no latency
77+
//
78+
var startTest = function (proxy, port) {
79+
testServers.noLatency = [];
80+
testServers.noLatency.push(startProxyServer('127.0.0.1', port, proxy));
81+
testServers.noLatency.push(startTargetServer(port));
82+
};
83+
84+
//
85+
// The test bootstrapper with some latency
86+
//
87+
var startTestWithLatency = function (proxy, port) {
88+
testServers.latency = [];
89+
testServers.latency.push(startLatentProxyServer('127.0.0.1', port, proxy, 2000));
90+
testServers.latency.push(startTargetServer(port));
91+
};
92+
93+
vows.describe('node-proxy').addBatch({
94+
"When an incoming request is proxied to the helloNode server" : {
95+
"with no latency" : {
96+
topic: function () {
97+
var proxy = new (NodeProxy);
98+
startTest(proxy, 8082);
99+
proxy.emitter.addListener('end', this.callback);
100+
101+
var client = http.createClient(8080, '127.0.0.1');
102+
var request = client.request('GET', '/');
103+
request.end();
104+
},
105+
"it should received 'hello world'": function (err, body) {
106+
assert.equal(body, 'hello world');
107+
testServers.noLatency.forEach(function (server) {
108+
server.close();
109+
})
110+
}
111+
},
112+
"with latency": {
113+
topic: function () {
114+
var proxy = new (NodeProxy);
115+
startTestWithLatency(proxy, 8083);
116+
proxy.emitter.addListener('end', this.callback);
117+
118+
var client = http.createClient(8081, '127.0.0.1');
119+
var request = client.request('GET', '/');
120+
request.end();
121+
},
122+
"it should receive 'hello world'": function (err, body) {
123+
assert.equal(body, 'hello world');
124+
testServers.latency.forEach(function (server) {
125+
server.close();
126+
})
127+
}
128+
}
129+
}
130+
}).export(module);

lib/asciimo.js

Whitespace-only changes.

vendor/asciimo

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 00f324d5e70a3c29321b48af04852e6b2b476d12

vendor/colors.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
colors.js
3+
4+
Copyright (c) 2010 Alexis Sellier (cloudhead) , Marak Squires
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
24+
*/
25+
26+
// prototypes the string object to have additional method calls that add terminal colors
27+
['bold', 'underline', 'italic', 'inverse', 'grey', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'].forEach(function (style) {
28+
Object.defineProperty(String.prototype, style, {
29+
get: function () {
30+
return stylize(this, style);
31+
}
32+
});
33+
});
34+
35+
// prototypes string with method "rainbow"
36+
// rainbow will apply a the color spectrum to a string, changing colors every letter
37+
Object.defineProperty(String.prototype, 'rainbow', {
38+
get: function () {
39+
var rainbowcolors = ['red','yellow','green','blue','magenta']; //RoY G BiV
40+
var exploded = this.split("");
41+
var i=0;
42+
exploded = exploded.map(function(letter) {
43+
if (letter==" ") {
44+
return letter;
45+
}
46+
else {
47+
return stylize(letter,rainbowcolors[i++ % rainbowcolors.length]);
48+
}
49+
});
50+
return exploded.join("");
51+
}
52+
});
53+
54+
function stylize(str, style) {
55+
var styles = {
56+
//styles
57+
'bold' : [1, 22],
58+
'italic' : [3, 23],
59+
'underline' : [4, 24],
60+
'inverse' : [7, 27],
61+
//grayscale
62+
'white' : [37, 39],
63+
'grey' : [90, 39],
64+
'black' : [90, 39],
65+
//colors
66+
'blue' : [34, 39],
67+
'cyan' : [36, 39],
68+
'green' : [32, 39],
69+
'magenta' : [35, 39],
70+
'red' : [31, 39],
71+
'yellow' : [33, 39]
72+
};
73+
return '\033[' + styles[style][0] + 'm' + str +
74+
'\033[' + styles[style][1] + 'm';
75+
};

0 commit comments

Comments
 (0)