node-http-proxy/examples/url-middleware2.js
2011-08-02 14:55:49 +10:00

31 lines
1.1 KiB
JavaScript

var util = require('util'),
colors = require('colors'),
http = require('http'),
httpProxy = require('http-proxy'),
Store = require('./lib/store')
http.createServer(new Store().handler()).listen(7531)
// Now we set up our proxy.
httpProxy.createServer(
// This is where our middlewares go, with any options desired - in this case,
// the list of routes/URLs and their destinations.
require('proxy-by-url')({
'/store': { port: 7531, host: 'localhost' },
'/': { port: 9000, host: 'localhost' }
})
).listen(8000);
//
// Target Http Server (to listen for requests on 'localhost')
//
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);
// And finally, some colored startup output.
util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow);