Commit 00014d62 authored by indexzero's avatar indexzero

[api test bin doc] Added bin script and simple logging

parent c06f4bf7
...@@ -41,6 +41,8 @@ There are several ways to use node-http-proxy; the library is designed to be fle ...@@ -41,6 +41,8 @@ There are several ways to use node-http-proxy; the library is designed to be fle
4. As a forward-proxy with a reverse proxy 4. As a forward-proxy with a reverse proxy
5. From the command-line as a proxy daemon 5. From the command-line as a proxy daemon
See the [demo](http://github.com/nodejitsu/node-http-proxy/blob/master/demo.js) for further examples.
### Setup a basic stand-alone proxy server ### Setup a basic stand-alone proxy server
<pre> <pre>
var http = require('http'), var http = require('http'),
...@@ -57,8 +59,6 @@ There are several ways to use node-http-proxy; the library is designed to be fle ...@@ -57,8 +59,6 @@ There are several ways to use node-http-proxy; the library is designed to be fle
}).listen(9000); }).listen(9000);
</pre> </pre>
See the [demo](http://github.com/nodejitsu/node-http-proxy/blob/master/demo.js) for further examples.
### Setup a stand-alone proxy server with custom server logic ### Setup a stand-alone proxy server with custom server logic
<pre> <pre>
var http = require('http'), var http = require('http'),
...@@ -151,6 +151,21 @@ Sometimes in addition to a reverse proxy, you may want your front-facing server ...@@ -151,6 +151,21 @@ Sometimes in addition to a reverse proxy, you may want your front-facing server
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'. 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'.
### Using node-http-proxy from the command line
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:
<pre>
usage: node-http-proxy [options]
All options should be set with the syntax --option=value
options:
--port PORT Port that the proxy server should run on
--target HOST:PORT Location of the server the proxy will target
--config OUTFILE Location of the configuration file for the proxy server
--silent Silence the log output from the proxy server
-h, --help You're staring at it
</pre>
<br/> <br/>
### Why doesn't node-http-proxy have more advanced features like x, y, or z? ### Why doesn't node-http-proxy have more advanced features like x, y, or z?
......
#!/usr/bin/env node
var path = require('path'),
fs = require('fs'),
eyes = require('eyes'),
sys = require('sys'),
argv = require('optimist').argv,
httpProxy = require('./../lib/node-http-proxy');
var help = [
"usage: node-http-proxy [options] ",
"",
"All options should be set with the syntax --option=value",
"",
"options:",
" --port PORT Port that the proxy server should run on",
" --target HOST:PORT Location of the server the proxy will target",
" --config OUTFILE Location of the configuration file for the proxy server",
" --silent Silence the log output from the proxy server",
" -h, --help You're staring at it"
];
if (argv.h || argv.help || Object.keys(argv).length === 2) {
sys.puts(help.join('\n'));
process.exit(0);
}
var location, config = {},
port = argv.port || 80,
target = argv.target;
//
// Check to see if we should silence the logs
//
config.silent = argv.silent || false;
//
// If we were passed a config, parse it
//
if (argv.config) {
try {
var data = fs.readFileSync(argv.config);
config = JSON.parse(data.toString());
} catch (ex) {
sys.puts('Error starting node-http-proxy: ' + ex);
process.exit(1);
}
}
//
// If we were passed a target, parse the url string
//
if (target) location = target.split(':');
//
// Create the server with the specified options
//
var server;
if (location) {
var targetPort = location.length === 1 ? 80 : location[1];
server = httpProxy.createServer(targetPort, location[0], config);
}
else {
server = httpProxy.createServer(config);
}
//
// Start the server
//
server.listen(port);
//
// Notify that the server is started
//
if (!config.silent) {
sys.puts('node-http-proxy server now listening on port: ' + port);
}
{
"silent": true,
"router": {
"localhost": "localhost:9000"
},
"forward": {
"port": 9001,
"host": "localhost"
}
}
\ No newline at end of file
...@@ -26,8 +26,9 @@ ...@@ -26,8 +26,9 @@
var sys = require('sys'), var sys = require('sys'),
http = require('http'), http = require('http'),
eyes = require('eyes'),
events = require('events'), events = require('events'),
pool = require('./../vendor/pool/main'), pool = require('pool'),
ProxyTable = require('./proxy-table').ProxyTable, ProxyTable = require('./proxy-table').ProxyTable,
min = 0, min = 0,
max = 100; max = 100;
...@@ -38,31 +39,48 @@ manager.setMinClients(min); ...@@ -38,31 +39,48 @@ manager.setMinClients(min);
manager.setMaxClients(max); manager.setMaxClients(max);
exports.createServer = function () { exports.createServer = function () {
var args, callback, port, host, options, proxyTable; var args, callback, port, host, forward,
silent, proxyTable, options = {};
args = Array.prototype.slice.call(arguments); args = Array.prototype.slice.call(arguments);
callback = typeof args[args.length - 1] === 'function' && args.pop(); callback = typeof args[args.length - 1] === 'function' && args.pop();
if (args.length >= 2) { if (args.length >= 2) {
port = args[0]; port = args[0];
host = args[1]; host = args[1];
if (args[2]) options = args[2]; options = args[2] || {};
} else if (args.length === 1) { } else if (args.length === 1) {
options = args[0]; options = args[0] || {};
if (!options.router && !callback) {
throw new Error('Cannot create server with no router and no callback');
}
} }
if (options && options.router) { router = options.router;
proxyTable = new ProxyTable(options.router); forward = options.forward;
silent = options.silent || true;
if (router) {
proxyTable = new ProxyTable(router, options.silent);
proxyTable.on('updateRoutes', function (routes) { proxyTable.on('updateRoutes', function (routes) {
server.emit('updateRoutes', routes); server.emit('updateRoutes', routes);
}); });
} }
var server = http.createServer(function (req, res) { var server = http.createServer(function (req, res) {
function log (message) {
if (!silent) {
sys.log(message);
}
}
var proxy = new HttpProxy(req, res); var proxy = new HttpProxy(req, res);
log('Incoming HTTP request to: ' + req.headers.host + req.url);
if (options && options.forward) { if (forward) {
var forward = new HttpProxy(req, res); var forward = new HttpProxy(req, res);
forward.forwardRequest(options.forward.port, options.forward.host); log('Forwarding HTTP request to: ' + forward.host + ':' + forward.port);
forward.forwardRequest(forward.port, forward.host);
} }
// If we were passed a callback to process the request // If we were passed a callback to process the request
...@@ -70,6 +88,7 @@ exports.createServer = function () { ...@@ -70,6 +88,7 @@ exports.createServer = function () {
if (callback) { if (callback) {
callback(req, res, proxy); callback(req, res, proxy);
} else if (port && host) { } else if (port && host) {
log('Proxying HTTP request to: ' + host + ':' + port);
proxy.proxyRequest(port, host); proxy.proxyRequest(port, host);
} else if (proxyTable) { } else if (proxyTable) {
proxyTable.proxyRequest(proxy); proxyTable.proxyRequest(proxy);
......
...@@ -28,8 +28,9 @@ var util = require('util'), ...@@ -28,8 +28,9 @@ var util = require('util'),
events = require('events'), events = require('events'),
fs = require('fs'); fs = require('fs');
var ProxyTable = function (router) { var ProxyTable = function (router, silent) {
events.EventEmitter.call(this); events.EventEmitter.call(this);
this.silent = silent || true;
if (typeof router === 'object') { if (typeof router === 'object') {
// If we are passed an object literal setup // If we are passed an object literal setup
...@@ -84,6 +85,10 @@ ProxyTable.prototype.proxyRequest = function (proxy) { ...@@ -84,6 +85,10 @@ ProxyTable.prototype.proxyRequest = function (proxy) {
host = location[0], host = location[0],
port = location.length === 1 ? 80 : location[1]; port = location.length === 1 ? 80 : location[1];
if (!this.silent) {
util.log('Proxy Table proxying request to: ' + host + ':' + port);
}
proxy.proxyRequest(port, host); proxy.proxyRequest(port, host);
return; return;
} }
......
...@@ -32,7 +32,7 @@ var badForwardOptions = { ...@@ -32,7 +32,7 @@ var badForwardOptions = {
} }
}; };
vows.describe('node-http-proxy').addBatch({ vows.describe('node-http-proxy/forward-proxy').addBatch({
"When using server created by httpProxy.createServer()": { "When using server created by httpProxy.createServer()": {
"with forwarding enabled": { "with forwarding enabled": {
topic: function () { topic: function () {
......
...@@ -33,7 +33,7 @@ var defaultOptions = { ...@@ -33,7 +33,7 @@ var defaultOptions = {
} }
}; };
vows.describe('proxy-table').addBatch({ vows.describe('node-http-proxy/proxy-table').addBatch({
"When using server created by httpProxy.createServer()": { "When using server created by httpProxy.createServer()": {
"when passed a routing table": { "when passed a routing table": {
topic: function () { topic: function () {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment