From 00014d624c052e7404ce96c7e06769440c4eae2a Mon Sep 17 00:00:00 2001 From: indexzero Date: Sun, 21 Nov 2010 23:43:25 -0500 Subject: [PATCH] [api test bin doc] Added bin script and simple logging --- README.md | 19 +++++++++- bin/node-http-proxy | 77 ++++++++++++++++++++++++++++++++++++++ config.sample.json | 10 +++++ lib/node-http-proxy.js | 35 +++++++++++++---- lib/proxy-table.js | 7 +++- test/forward-proxy-test.js | 2 +- test/proxy-table-test.js | 2 +- 7 files changed, 139 insertions(+), 13 deletions(-) create mode 100755 bin/node-http-proxy create mode 100644 config.sample.json diff --git a/README.md b/README.md index ff912ce86..48e76d618 100644 --- a/README.md +++ b/README.md @@ -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
   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);
 
-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
   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:
+
+  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
+
+
### Why doesn't node-http-proxy have more advanced features like x, y, or z? diff --git a/bin/node-http-proxy b/bin/node-http-proxy new file mode 100755 index 000000000..c369c747c --- /dev/null +++ b/bin/node-http-proxy @@ -0,0 +1,77 @@ +#!/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); +} diff --git a/config.sample.json b/config.sample.json new file mode 100644 index 000000000..dc68a52f9 --- /dev/null +++ b/config.sample.json @@ -0,0 +1,10 @@ +{ + "silent": true, + "router": { + "localhost": "localhost:9000" + }, + "forward": { + "port": 9001, + "host": "localhost" + } +} \ No newline at end of file diff --git a/lib/node-http-proxy.js b/lib/node-http-proxy.js index 7f4575199..1fb53ee03 100644 --- a/lib/node-http-proxy.js +++ b/lib/node-http-proxy.js @@ -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); diff --git a/lib/proxy-table.js b/lib/proxy-table.js index c7d4ea3b6..da0153272 100644 --- a/lib/proxy-table.js +++ b/lib/proxy-table.js @@ -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; } diff --git a/test/forward-proxy-test.js b/test/forward-proxy-test.js index 08db99bdf..9d3bfab5e 100644 --- a/test/forward-proxy-test.js +++ b/test/forward-proxy-test.js @@ -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 () { diff --git a/test/proxy-table-test.js b/test/proxy-table-test.js index 4f95c2051..09e29cefd 100644 --- a/test/proxy-table-test.js +++ b/test/proxy-table-test.js @@ -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 () {