From 7a3f6dfbcc80ba32fa81004438c637e8d29eb029 Mon Sep 17 00:00:00 2001 From: cronopio Date: Mon, 11 Nov 2013 11:22:04 -0500 Subject: [PATCH] [examples] added forward example --- examples/forward-proxy.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 examples/forward-proxy.js diff --git a/examples/forward-proxy.js b/examples/forward-proxy.js new file mode 100644 index 0000000..4cb516f --- /dev/null +++ b/examples/forward-proxy.js @@ -0,0 +1,33 @@ +var http = require('http'), + httpProxy = require('../lib/http-proxy'); + +// +// Target Http Server +// +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); +console.log("Web server listening on port 9000"); + +// +// Target Http Forwarding Server +// +http.createServer(function (req, res) { + console.log('Receiving forward for: ' + req.url); + res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.write('request successfully forwarded to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2)); + res.end(); +}).listen(9001); + +// +// Basic Http Proxy Server +// Forward example: send requests without care about response +// +httpProxy.createServer({ + target: 'http://localhost:9000', + forward: 'http://localhost:9001' +}).listen(8000) +console.log("Proxy server listening on port 8000"); +