Skip to content

Commit 212009d

Browse files
committed
[doc api] Update README.md and CHANGELOG.md for v0.5.0. Update bin/node-http-proxy to read files specified in config.https
1 parent bf68dc3 commit 212009d

11 files changed

+141
-23
lines changed

CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
## Version 0.5.0 - 4/15/2011
44
- Remove winston in favor of custom events (indexzero)
5-
5+
- Add x-forwarded-for Header (indexzero)
6+
- Fix WebSocket support (indexzero)
7+
- Add tests / examples for WebSocket support (indexzero)
8+
- Update .proxyRequest() and .proxyWebSocketRequest() APIs (indexzero)
9+
- Add HTTPS support (indexzero)
10+
- Add tests / examples for HTTPS support (indexzero)
611

712
## Version 0.4.1 - 3/20/2011
813
- Include missing dependency in package.json (indexzero)

README.md

+106-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# node-http-proxy - v0.4.1
1+
# node-http-proxy - v0.5.0
22

3-
<img src = "http://i.imgur.com/dSSUX.png"/>
3+
<img src="http://i.imgur.com/9BrV7.png" />
44

55
## Battle-hardened node.js http proxy
66

@@ -10,6 +10,8 @@
1010
* Can be used as a CommonJS module in node.js
1111
* Uses event buffering to support application latency in proxied requests
1212
* Reverse or Forward Proxy based on simple JSON-based configuration
13+
* Supports [WebSockets][1]
14+
* Supports [HTTPS][2]
1315
* Minimal request overhead and latency
1416
* Full suite of functional tests
1517
* Battled-hardened through __production usage__ @ [nodejitsu.com][0]
@@ -40,7 +42,13 @@ There are several ways to use node-http-proxy; the library is designed to be fle
4042
4. As a forward-proxy with a reverse proxy
4143
5. From the command-line as a long running process
4244

43-
See the [demo](http://github.com/nodejitsu/node-http-proxy/blob/master/demo.js) for further examples.
45+
In each of these scenarios node-http-proxy can handle any of these types of requests:
46+
47+
1. HTTP Requests (http://)
48+
2. HTTPS Requests (https://)
49+
3. WebSocket Requests (ws://)
50+
51+
See the [examples][3] for more working sample code.
4452

4553
### Setup a basic stand-alone proxy server
4654
<pre>
@@ -55,7 +63,7 @@ See the [demo](http://github.com/nodejitsu/node-http-proxy/blob/master/demo.js)
5563
// Create your target server
5664
//
5765
http.createServer(function (req, res) {
58-
res.writeHead(200, {'Content-Type': 'text/plain'});
66+
res.writeHead(200, { 'Content-Type': 'text/plain' });
5967
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
6068
res.end();
6169
}).listen(9000);
@@ -73,11 +81,14 @@ See the [demo](http://github.com/nodejitsu/node-http-proxy/blob/master/demo.js)
7381
//
7482
// Put your custom server logic here
7583
//
76-
proxy.proxyRequest(req, res, 9000, 'localhost');
84+
proxy.proxyRequest(req, res, {
85+
host: 'localhost',
86+
port: 9000
87+
});
7788
}).listen(8000);
7889

7990
http.createServer(function (req, res) {
80-
res.writeHead(200, {'Content-Type': 'text/plain'});
91+
res.writeHead(200, { 'Content-Type': 'text/plain' });
8192
res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
8293
res.end();
8394
}).listen(9000);
@@ -103,12 +114,16 @@ See the [demo](http://github.com/nodejitsu/node-http-proxy/blob/master/demo.js)
103114
// performing async actions before proxying a request
104115
//
105116
setTimeout(function () {
106-
proxy.proxyRequest(req, res, 9000, 'localhost', buffer);
117+
proxy.proxyRequest(req, res, {
118+
host: 'localhost',
119+
port: 9000,
120+
buffer: buffer
121+
});
107122
}, 2000);
108123
}).listen(8000);
109124

110125
http.createServer(function (req, res) {
111-
res.writeHead(200, {'Content-Type': 'text/plain'});
126+
res.writeHead(200, { 'Content-Type': 'text/plain' });
112127
res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
113128
res.end();
114129
}).listen(9000);
@@ -131,11 +146,14 @@ See the [demo](http://github.com/nodejitsu/node-http-proxy/blob/master/demo.js)
131146
//
132147
// Put your custom server logic here, then proxy
133148
//
134-
proxy.proxyRequest(req, res, 9000, 'localhost');
149+
proxy.proxyRequest(req, res, {
150+
host: 'localhost',
151+
port: 9000
152+
});
135153
}).listen(8001);
136154

137155
http.createServer(function (req, res) {
138-
res.writeHead(200, {'Content-Type': 'text/plain'});
156+
res.writeHead(200, { 'Content-Type': 'text/plain' });
139157
res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
140158
res.end();
141159
}).listen(9000);
@@ -172,7 +190,7 @@ As mentioned in the previous section, all routes passes to the ProxyTable are by
172190
}
173191
</pre>
174192

175-
Notice here that I have not included paths on the individual domains because this is not possible when using only the HTTP 'Host' header. Care to learn more? See [RFC2616: HTTP/1.1, Section 14.23, "Host"][1].
193+
Notice here that I have not included paths on the individual domains because this is not possible when using only the HTTP 'Host' header. Care to learn more? See [RFC2616: HTTP/1.1, Section 14.23, "Host"][4].
176194

177195
### Proxy requests with an additional forward proxy
178196
Sometimes in addition to a reverse proxy, you may want your front-facing server to forward traffic to another location. For example, if you wanted to load test your staging environment. This is possible when using node-http-proxy using similar JSON-based configuration to a proxy table:
@@ -203,6 +221,78 @@ When you install this package with npm, a node-http-proxy binary will become ava
203221
-h, --help You're staring at it
204222
</pre>
205223

224+
### Proxying over HTTPS
225+
You have all the full flexibility of node-http-proxy offers in HTTPS as well as HTTP. The two basic scenarios are: with a stand-alone proxy server or in conjunction with another HTTPS server.
226+
<pre>
227+
var fs = require('fs'),
228+
https = require('https'),
229+
httpProxy = require('httpProxy');
230+
231+
var options = {
232+
https: {
233+
key: fs.readFileSync('path/to/your/key.pem', 'utf8'),
234+
cert: fs.readFileSync('path/to/your/cert.pem', 'utf8')
235+
}
236+
};
237+
238+
//
239+
// Create a standalone HTTPS proxy server
240+
//
241+
httpProxy.createServer(8000, 'localhost', options).listen(8001);
242+
243+
//
244+
// Create an instance of HttpProxy to use with another HTTPS server
245+
//
246+
var proxy = new httpProxy.HttpProxy({ https: true });
247+
https.createServer(options.https, function (req, res) {
248+
proxy.proxyRequest(req, res, {
249+
host: 'localhost',
250+
port: 8000
251+
})
252+
}).listen(8002);
253+
254+
//
255+
// Create the target HTTPS server for both cases
256+
//
257+
https.createServer(options.https, function (req, res) {
258+
res.writeHead(200, { 'Content-Type': 'text/plain' });
259+
res.write('hello https\n');
260+
res.end();
261+
}).listen(8000);
262+
</pre>
263+
264+
### Proxying WebSockets
265+
Websockets are handled automatically when using the `httpProxy.createServer()`, but if you want to use it in conjunction with a stand-alone HTTP + WebSocket (such as [socket.io][5]) server here's how:
266+
<pre>
267+
var http = require('http'),
268+
httpProxy = require('httpProxy');
269+
270+
//
271+
// Create an instance of node-http-proxy
272+
//
273+
var proxy = new httpProxy.HttpProxy();
274+
275+
var server = http.createServer(function (req, res) {
276+
//
277+
// Proxy normal HTTP requests
278+
//
279+
proxy.proxyRequest(req, res, {
280+
host: 'localhost',
281+
port: 8000
282+
})
283+
});
284+
285+
server.on('upgrade', function(req, socket, head) {
286+
//
287+
// Proxy websocket requests too
288+
//
289+
proxy.proxyWebSocketRequest(req, socket, head, {
290+
host: 'localhost',
291+
port: 8000
292+
});
293+
});
294+
</pre>
295+
206296
<br/>
207297
### Why doesn't node-http-proxy have more advanced features like x, y, or z?
208298

@@ -235,4 +325,8 @@ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
235325
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
236326

237327
[0]: http://nodejitsu.com
238-
[1]: http://www.ietf.org/rfc/rfc2616.txt
328+
[1]: https://github.com/nodejitsu/node-http-proxy/blob/master/examples/web-socket-proxy.js
329+
[2]: https://github.com/nodejitsu/node-http-proxy/blob/master/examples/basic-proxy-https.js
330+
[3]: https://github.com/nodejitsu/node-http-proxy/tree/v0.5.0/examples
331+
[4]: http://www.ietf.org/rfc/rfc2616.txt
332+
[5]: http://socket.io

bin/node-http-proxy

+9
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ if (argv.config) {
4040
}
4141
}
4242

43+
//
44+
// If `config.https` is set, then load those files into the config options.
45+
//
46+
if (options.config) {
47+
Object.keys(options.config).forEach(function (key) {
48+
options.config[key] = fs.readFileSync(options.config[key], 'utf8');
49+
});
50+
}
51+
4352
//
4453
// Check to see if we should silence the logs
4554
//

examples/basic-proxy-https.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var opts = helpers.loadHttps();
3737
// Crete the target HTTPS server
3838
//
3939
https.createServer(opts, function (req, res) {
40-
res.writeHead(200, {'Content-Type': 'text/plain'});
40+
res.writeHead(200, { 'Content-Type': 'text/plain' });
4141
res.write('hello https\n');
4242
res.end();
4343
}).listen(8000);

examples/basic-proxy.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,17 @@ var util = require('util'),
2828
colors = require('colors')
2929
http = require('http'),
3030
httpProxy = require('./../lib/node-http-proxy');
31-
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+
3242
//
3343
// Basic Http Proxy Server
3444
//
@@ -38,7 +48,7 @@ httpProxy.createServer(9000, 'localhost').listen(8000);
3848
// Target Http Server
3949
//
4050
http.createServer(function (req, res) {
41-
res.writeHead(200, {'Content-Type': 'text/plain'});
51+
res.writeHead(200, { 'Content-Type': 'text/plain' });
4252
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
4353
res.end();
4454
}).listen(9000);

examples/forward-proxy.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ httpProxy.createServer(9000, 'localhost', {
4343
// Target Http Server
4444
//
4545
http.createServer(function (req, res) {
46-
res.writeHead(200, {'Content-Type': 'text/plain'});
46+
res.writeHead(200, { 'Content-Type': 'text/plain' });
4747
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
4848
res.end();
4949
}).listen(9000);
@@ -53,7 +53,7 @@ http.createServer(function (req, res) {
5353
//
5454
http.createServer(function (req, res) {
5555
util.puts('Receiving forward for: ' + req.url)
56-
res.writeHead(200, {'Content-Type': 'text/plain'});
56+
res.writeHead(200, { 'Content-Type': 'text/plain' });
5757
res.write('request successfully forwarded to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
5858
res.end();
5959
}).listen(9001);

examples/latent-proxy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ httpProxy.createServer(function (req, res, proxy) {
4747
// Target Http Server
4848
//
4949
http.createServer(function (req, res) {
50-
res.writeHead(200, {'Content-Type': 'text/plain'});
50+
res.writeHead(200, { 'Content-Type': 'text/plain' });
5151
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
5252
res.end();
5353
}).listen(9000);

examples/proxy-table.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ httpProxy.createServer({
4242
// Target Http Server
4343
//
4444
http.createServer(function (req, res) {
45-
res.writeHead(200, {'Content-Type': 'text/plain'});
45+
res.writeHead(200, { 'Content-Type': 'text/plain' });
4646
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
4747
res.end();
4848
}).listen(9000);

examples/standalone-proxy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ http.createServer(function (req, res) {
4848
// Target Http Server
4949
//
5050
http.createServer(function (req, res) {
51-
res.writeHead(200, {'Content-Type': 'text/plain'});
51+
res.writeHead(200, { 'Content-Type': 'text/plain' });
5252
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
5353
res.end();
5454
}).listen(9000);

lib/node-http-proxy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ exports.createServer = function () {
192192
// Set the proxy on the server so it is available
193193
// to the consumer of the server
194194
//
195-
//server.proxy = proxy;
195+
server.proxy = proxy;
196196

197197
return server;
198198
};

test/helpers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ TestRunner.prototype.startProxyServerWithForwarding = function (port, targetPort
198198
//
199199
TestRunner.prototype.startTargetServer = function (port, output, callback) {
200200
var that = this, targetServer, handler = function (req, res) {
201-
res.writeHead(200, {'Content-Type': 'text/plain'});
201+
res.writeHead(200, { 'Content-Type': 'text/plain' });
202202
res.write(output);
203203
res.end();
204204
};

0 commit comments

Comments
 (0)