Skip to content

Instantly share code, notes, and snippets.

@oeeckhoutte
Created November 6, 2012 19:37
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 oeeckhoutte/4026965 to your computer and use it in GitHub Desktop.
Save oeeckhoutte/4026965 to your computer and use it in GitHub Desktop.
[Node] Simple reverse Proxy
var util = require('util'); //Used to debug - inspect cmd
d = function (debugString) {
console.log(util.inspect(debugString, true, 6, true));
};
t = function (traceString) {
console.log('---> ' + traceString);
};
var http = require('http');
var httpProxy = require('http-proxy');
var myDevMachineIP = "10.171.182.187"; //local IP
var myDevMachinePort = "3000";
/****************** Using HTTP Only ******************************/
// var proxy = new httpProxy.RoutingProxy();
// var server = http.createServer(function (req, res) {
// //
// // Put your custom server logic here, then proxy
// //
// proxy.proxyRequest(req, res, {
// host: myDevMachineIP,
// port: myDevMachinePort
// });
// }).listen(3000);
/**************************************************************/
/********************** Websocket support *************************/
//
// Create a new instance of HttProxy to use in your server
//
var proxy = new httpProxy.HttpProxy({
target: {
host: myDevMachineIP,
port: myDevMachinePort
}
});
//
// Create a regular http server and proxy its handler
//
var server = http.createServer(function (req, res) {
proxy.proxyRequest(req, res);
});
server.on('upgrade', function(req, socket, head) {
//
// Proxy websocket requests too
//
proxy.proxyWebSocketRequest(req, socket, head);
});
server.listen(3000);
/***************************************************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment