Skip to content

Commit 00014d6

Browse files
committed
[api test bin doc] Added bin script and simple logging
1 parent c06f4bf commit 00014d6

7 files changed

+139
-13
lines changed

README.md

+17-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ There are several ways to use node-http-proxy; the library is designed to be fle
4141
4. As a forward-proxy with a reverse proxy
4242
5. From the command-line as a proxy daemon
4343

44+
See the [demo](http://github.com/nodejitsu/node-http-proxy/blob/master/demo.js) for further examples.
45+
4446
### Setup a basic stand-alone proxy server
4547
<pre>
4648
var http = require('http'),
@@ -57,8 +59,6 @@ There are several ways to use node-http-proxy; the library is designed to be fle
5759
}).listen(9000);
5860
</pre>
5961

60-
See the [demo](http://github.com/nodejitsu/node-http-proxy/blob/master/demo.js) for further examples.
61-
6262
### Setup a stand-alone proxy server with custom server logic
6363
<pre>
6464
var http = require('http'),
@@ -151,6 +151,21 @@ Sometimes in addition to a reverse proxy, you may want your front-facing server
151151

152152
The forwarding option can be used in conjunction with the proxy table options by simply including both the 'forward' and 'router' properties in the options passed to 'createServer'.
153153

154+
### Using node-http-proxy from the command line
155+
When you install this package with npm, a node-http-proxy binary will become available to you. Using this binary is easy with some simple options:
156+
<pre>
157+
usage: node-http-proxy [options]
158+
159+
All options should be set with the syntax --option=value
160+
161+
options:
162+
--port PORT Port that the proxy server should run on
163+
--target HOST:PORT Location of the server the proxy will target
164+
--config OUTFILE Location of the configuration file for the proxy server
165+
--silent Silence the log output from the proxy server
166+
-h, --help You're staring at it
167+
</pre>
168+
154169
<br/>
155170
### Why doesn't node-http-proxy have more advanced features like x, y, or z?
156171

bin/node-http-proxy

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env node
2+
3+
var path = require('path'),
4+
fs = require('fs'),
5+
eyes = require('eyes'),
6+
sys = require('sys'),
7+
argv = require('optimist').argv,
8+
httpProxy = require('./../lib/node-http-proxy');
9+
10+
var help = [
11+
"usage: node-http-proxy [options] ",
12+
"",
13+
"All options should be set with the syntax --option=value",
14+
"",
15+
"options:",
16+
" --port PORT Port that the proxy server should run on",
17+
" --target HOST:PORT Location of the server the proxy will target",
18+
" --config OUTFILE Location of the configuration file for the proxy server",
19+
" --silent Silence the log output from the proxy server",
20+
" -h, --help You're staring at it"
21+
];
22+
23+
if (argv.h || argv.help || Object.keys(argv).length === 2) {
24+
sys.puts(help.join('\n'));
25+
process.exit(0);
26+
}
27+
28+
var location, config = {},
29+
port = argv.port || 80,
30+
target = argv.target;
31+
32+
//
33+
// Check to see if we should silence the logs
34+
//
35+
config.silent = argv.silent || false;
36+
37+
//
38+
// If we were passed a config, parse it
39+
//
40+
if (argv.config) {
41+
try {
42+
var data = fs.readFileSync(argv.config);
43+
config = JSON.parse(data.toString());
44+
} catch (ex) {
45+
sys.puts('Error starting node-http-proxy: ' + ex);
46+
process.exit(1);
47+
}
48+
}
49+
50+
//
51+
// If we were passed a target, parse the url string
52+
//
53+
if (target) location = target.split(':');
54+
55+
//
56+
// Create the server with the specified options
57+
//
58+
var server;
59+
if (location) {
60+
var targetPort = location.length === 1 ? 80 : location[1];
61+
server = httpProxy.createServer(targetPort, location[0], config);
62+
}
63+
else {
64+
server = httpProxy.createServer(config);
65+
}
66+
67+
//
68+
// Start the server
69+
//
70+
server.listen(port);
71+
72+
//
73+
// Notify that the server is started
74+
//
75+
if (!config.silent) {
76+
sys.puts('node-http-proxy server now listening on port: ' + port);
77+
}

config.sample.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"silent": true,
3+
"router": {
4+
"localhost": "localhost:9000"
5+
},
6+
"forward": {
7+
"port": 9001,
8+
"host": "localhost"
9+
}
10+
}

lib/node-http-proxy.js

+27-8
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626

2727
var sys = require('sys'),
2828
http = require('http'),
29+
eyes = require('eyes'),
2930
events = require('events'),
30-
pool = require('./../vendor/pool/main'),
31+
pool = require('pool'),
3132
ProxyTable = require('./proxy-table').ProxyTable,
3233
min = 0,
3334
max = 100;
@@ -38,38 +39,56 @@ manager.setMinClients(min);
3839
manager.setMaxClients(max);
3940

4041
exports.createServer = function () {
41-
var args, callback, port, host, options, proxyTable;
42+
var args, callback, port, host, forward,
43+
silent, proxyTable, options = {};
44+
4245
args = Array.prototype.slice.call(arguments);
4346
callback = typeof args[args.length - 1] === 'function' && args.pop();
4447

4548
if (args.length >= 2) {
4649
port = args[0];
4750
host = args[1];
48-
if (args[2]) options = args[2];
51+
options = args[2] || {};
4952
} else if (args.length === 1) {
50-
options = args[0];
53+
options = args[0] || {};
54+
if (!options.router && !callback) {
55+
throw new Error('Cannot create server with no router and no callback');
56+
}
5157
}
5258

53-
if (options && options.router) {
54-
proxyTable = new ProxyTable(options.router);
59+
router = options.router;
60+
forward = options.forward;
61+
silent = options.silent || true;
62+
63+
if (router) {
64+
proxyTable = new ProxyTable(router, options.silent);
5565
proxyTable.on('updateRoutes', function (routes) {
5666
server.emit('updateRoutes', routes);
5767
});
5868
}
5969

6070
var server = http.createServer(function (req, res) {
71+
function log (message) {
72+
if (!silent) {
73+
sys.log(message);
74+
}
75+
}
76+
6177
var proxy = new HttpProxy(req, res);
78+
log('Incoming HTTP request to: ' + req.headers.host + req.url);
6279

63-
if (options && options.forward) {
80+
if (forward) {
6481
var forward = new HttpProxy(req, res);
65-
forward.forwardRequest(options.forward.port, options.forward.host);
82+
log('Forwarding HTTP request to: ' + forward.host + ':' + forward.port);
83+
forward.forwardRequest(forward.port, forward.host);
6684
}
6785

6886
// If we were passed a callback to process the request
6987
// or response in some way, then call it.
7088
if (callback) {
7189
callback(req, res, proxy);
7290
} else if (port && host) {
91+
log('Proxying HTTP request to: ' + host + ':' + port);
7392
proxy.proxyRequest(port, host);
7493
} else if (proxyTable) {
7594
proxyTable.proxyRequest(proxy);

lib/proxy-table.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ var util = require('util'),
2828
events = require('events'),
2929
fs = require('fs');
3030

31-
var ProxyTable = function (router) {
31+
var ProxyTable = function (router, silent) {
3232
events.EventEmitter.call(this);
33+
this.silent = silent || true;
3334

3435
if (typeof router === 'object') {
3536
// If we are passed an object literal setup
@@ -84,6 +85,10 @@ ProxyTable.prototype.proxyRequest = function (proxy) {
8485
host = location[0],
8586
port = location.length === 1 ? 80 : location[1];
8687

88+
if (!this.silent) {
89+
util.log('Proxy Table proxying request to: ' + host + ':' + port);
90+
}
91+
8792
proxy.proxyRequest(port, host);
8893
return;
8994
}

test/forward-proxy-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var badForwardOptions = {
3232
}
3333
};
3434

35-
vows.describe('node-http-proxy').addBatch({
35+
vows.describe('node-http-proxy/forward-proxy').addBatch({
3636
"When using server created by httpProxy.createServer()": {
3737
"with forwarding enabled": {
3838
topic: function () {

test/proxy-table-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var defaultOptions = {
3333
}
3434
};
3535

36-
vows.describe('proxy-table').addBatch({
36+
vows.describe('node-http-proxy/proxy-table').addBatch({
3737
"When using server created by httpProxy.createServer()": {
3838
"when passed a routing table": {
3939
topic: function () {

0 commit comments

Comments
 (0)