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
4. As a forward-proxy with a reverse proxy
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
<pre>
var http = require('http'),
......@@ -57,8 +59,6 @@ There are several ways to use node-http-proxy; the library is designed to be fle
}).listen(9000);
</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
<pre>
var http = require('http'),
......@@ -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'.
### 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/>
### 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 @@
var sys = require('sys'),
http = require('http'),
eyes = require('eyes'),
events = require('events'),
pool = require('./../vendor/pool/main'),
pool = require('pool'),
ProxyTable = require('./proxy-table').ProxyTable,
min = 0,
max = 100;
......@@ -38,31 +39,48 @@ manager.setMinClients(min);
manager.setMaxClients(max);
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);
callback = typeof args[args.length - 1] === 'function' && args.pop();
if (args.length >= 2) {
port = args[0];
host = args[1];
if (args[2]) options = args[2];
options = args[2] || {};
} 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) {
proxyTable = new ProxyTable(options.router);
router = options.router;
forward = options.forward;
silent = options.silent || true;
if (router) {
proxyTable = new ProxyTable(router, options.silent);
proxyTable.on('updateRoutes', function (routes) {
server.emit('updateRoutes', routes);
});
}
var server = http.createServer(function (req, res) {
function log (message) {
if (!silent) {
sys.log(message);
}
}
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);
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
......@@ -70,6 +88,7 @@ exports.createServer = function () {
if (callback) {
callback(req, res, proxy);
} else if (port && host) {
log('Proxying HTTP request to: ' + host + ':' + port);
proxy.proxyRequest(port, host);
} else if (proxyTable) {
proxyTable.proxyRequest(proxy);
......
......@@ -28,8 +28,9 @@ var util = require('util'),
events = require('events'),
fs = require('fs');
var ProxyTable = function (router) {
var ProxyTable = function (router, silent) {
events.EventEmitter.call(this);
this.silent = silent || true;
if (typeof router === 'object') {
// If we are passed an object literal setup
......@@ -84,6 +85,10 @@ ProxyTable.prototype.proxyRequest = function (proxy) {
host = location[0],
port = location.length === 1 ? 80 : location[1];
if (!this.silent) {
util.log('Proxy Table proxying request to: ' + host + ':' + port);
}
proxy.proxyRequest(port, host);
return;
}
......
......@@ -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()": {
"with forwarding enabled": {
topic: function () {
......
......@@ -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 passed a routing table": {
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