Skip to content

Instantly share code, notes, and snippets.

@jrichardsz
Forked from wiredmax/server.js
Created October 2, 2018 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrichardsz/0750f9a8d47efd585a6c93d09bed32de to your computer and use it in GitHub Desktop.
Save jrichardsz/0750f9a8d47efd585a6c93d09bed32de 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