Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Can this project be used to implement a standard proxy with custom functionality? #452

Closed
fabiosantoscode opened this Issue Aug 20, 2013 · 4 comments

Comments

Projects
None yet
2 participants

I have been trying to use the nodeJS module to create a web proxy, but I have been unable to. Is this project really usable for creating web proxies (such as those we use to access the web anonymously) or can it only be used for reverse proxies for load balancing and whatnot?

What I am trying to do is a proxy server which you can configure in your browser's internet options, which will proxy all requests but intercept and respond to some with custom data.

I intend to implement a proxy server for easily swapping remote code with local files, which I intend to use in order to ease front-end JS development. Instead of reproducing whole environments from a remote site in my own machine, I intend to simply access the remote website and have this software swap "main.js" with a file on my machine.

Is this possible to do with this module?

Thanks ;)

I spent some more time tinkering and was able to do what I wanted. Now I can change the frontend scripts without access to my colleagues' full setup, which is great.

Thanks for the wonderful library :)

Hey @fabiosantoscode! How did you handle the static resources? For me only the main html file gets loaded. Here's a quick example of my code:

var express = require('express');
var https = require('https');
var http = require('http'); 
var url= require('url');
var path = require('path');
var logger = require('morgan');
var httpProxy = require('http-proxy');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var app = express();

var selects = [];
var simpleselect = {};

app.use('/app', express.static(__dirname));
app.use(cookieParser());

var apiProxy = httpProxy.createProxyServer();

app.use(function (req,res,next) {
if(req.path.indexOf('/proxy/')!=-1){
   console.log('proxy request received for '+req.path);
   next();
}
else{
   console.log('non-proxy request received');
   app.use(bodyParser.json());
   app.use(bodyParser.urlencoded({ extended: false }));
   next();
 }
 })

app.get('/',function(req,res){
  res.sendStatus(200)
})

app.get(/\/proxy\/./, function(req, res){
var targetwebsite=req.path.split('/proxy/')[1];
console.log("Complete path "+req.path);
if(targetwebsite.indexOf('https://')!=-1){
   console.log('https '+targetwebsite);
   apiProxy.web(req, res, { 
      target: targetwebsite,
      agent: https.globalAgent,
      headers:{
         host: url.parse(targetwebsite).hostname
      },
      prependPath:false,
      ignorePath:true,
      changeOrigin:true,
      autoRewrite: true,
      hostRewrite:true,
      protocolRewrite:true
  });
}
else{
   console.log('non https '+targetwebsite);
   apiProxy.web(req, res, { 
     target: targetwebsite,
      agent: http.globalAgent,
      headers:{
        host: url.parse(targetwebsite).hostname
      },
      prependPath:false,
      ignorePath:true,
      changeOrigin:true,
      autoRewrite: true,
      hostRewrite:true,
      protocolRewrite:true
   });
 }
});


var port = process.env.PORT || 3000;
app.listen(port);

I didn't pass so many options. The ignorePath one is catching my eye, could the proxy be resolving /static/* to /?

While debugging this you might find it useful to look at your server's access log and see if the proxy is requesting the right stuff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment