Created
September 16, 2014 23:08
-
-
Save wiredmax/8bbb96be4e320b7d584b to your computer and use it in GitHub Desktop.
Simple NodeJS proxy server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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