Skip to content

Instantly share code, notes, and snippets.

@wiredmax
Created September 16, 2014 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wiredmax/8bbb96be4e320b7d584b to your computer and use it in GitHub Desktop.
Save wiredmax/8bbb96be4e320b7d584b to your computer and use it in GitHub Desktop.
Simple NodeJS proxy server
var http = require('http'),
httpProxy = require('http-proxy');
//
// Create a proxy server with custom application logic
//
var proxy = httpProxy.createProxyServer({});
//
// Create your custom server and just call `proxy.web()` to proxy
// a web request to the target passed in the options
// also you can use `proxy.ws()` to proxy a websockets request
//
var server = require('http').createServer(function(req, res) {
// You can define here your custom logic to handle the request
// and then proxy the request.
var targets = {
'foo.bar.com': 'localhost:3001'
}
proxy.web(req, res, {
xfwd: true,
target: 'http://' + targets[req.headers.host] + ''
});
});
console.log("Proxy listening on port 80")
server.listen(80);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment