indexzero
2/19/2014 - 6:44 AM

URL to host rewrite proxying with node-http-proxy

URL to host rewrite proxying with node-http-proxy

var httpProxy = require('http-proxy')

var proxy = httpProxy.createProxy();

var fulltld = 'my-own-domain.com';

var options = {
  '/first-target':  'first-target',
  '/second-target': 'second-target'
}

require('http').createServer(function(req, res) {
  if (!options[req.url]) {
    return res.end(400);
  }

  //
  // Need to change the URL here.
  //
  var dest = req.url;
  req.url = '/';
  
  proxy.web(req, res, {
    target: options[req.url] + '.' + fulltld;
  });
}).listen(8000);