From 8eaec3507456731c1138c0b8ebb4e51dedc7c300 Mon Sep 17 00:00:00 2001 From: Charlie McConnell Date: Thu, 28 Jul 2011 13:43:40 -0700 Subject: [PATCH] [minor] Added body decoder middleware example. Needs fixing. --- examples/body-decoder.js | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 examples/body-decoder.js diff --git a/examples/body-decoder.js b/examples/body-decoder.js new file mode 100644 index 0000000..e4a3267 --- /dev/null +++ b/examples/body-decoder.js @@ -0,0 +1,43 @@ +#!/usr/local/bin/node + +var httpProxy = require('http-proxy'), + http = require('http'), + util = require('util'), + colors = require('colors'); + + +exports.bodyMod = function () { + console.log('middleware has been started.'.green); + return function (req, res, next) { + var proxy = next, + total = ''; + + req.on('data', function (data) { + console.log('ON DATA') + total += data; + }); + req.on('end', function () { + console.log('ON END') + console.log(total); + // This line, uncommented, hangs forever. + // proxy.proxyRequest(req, res, { port: 9000, host: 'localhost' }); + // The following also hangs forever. + // next.proxyRequest(req, res, { port: 9000, host: 'localhost' }); + }) + // The following fires just fine. + //proxy.proxyRequest(req, res, { port: 9000, host: 'localhost' }); + console.log('request proxied...'.blue); + } +} + +var proxyServer = httpProxy.createServer( + // Your middleware stack goes here. + exports.bodyMod() +).listen(8000); + + +var httpServer = http.createServer(function (req, res) { + res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2)); + res.end(); +}).listen(9000); \ No newline at end of file