0

I'm trying to set up a proxy server that should return the http requests from any page that I'm accessing.

Basically, if I navigate to www.google.com then I'm expecting to get the following requests:

enter image description here

Is this achievable using node-http-proxy module?

I've tried the following code, but can't figure out how to get the requests..

var http = require('http'),  
    httpProxy = require('http-proxy');

//
// Create a proxy server with custom application logic
//
httpProxy.createServer(function (req, res, proxy) {  
  //
  // Put your custom server logic here
  //

  proxy.proxyRequest(req, res, {
    host: 'localhost',
    port: 9000
  });
}).listen(8000);

http.createServer(function (req, res) {  
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
  res.end();
}).listen(9000);

UPDATE:
I configured your browser to use my proxy server, and changed the code as follows:

var http = require('http'),
    httpProxy = require('http-proxy');

//
// Create a proxy server with custom application logic
//
var proxy = httpProxy.createServer(function (req, res, proxy) {  
  //
  // Put your custom server logic here
  //
  proxy.proxyRequest(req, res, {
    host: 'localhost',
    port: 9000
  });
})

proxy.listen(8000);

proxy.on('proxyReq', function(proxyReq, req, res, options) {
  console.log(req.url);
  console.log(proxyReq.url);
});

http.createServer(function(req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
  res.end();
}).listen(9000);

But there are no logs in the console when I'm accessing different websites

1 Answer 1

1

You will probably have to configure your browser to use your proxy server:

For chrome,

  1. go to settings,
  2. search for "proxy",
  3. click "change proxy settings",
  4. click LAN settings,
  5. and then add information about your proxy server.

I am not sure if you need to restart the browser, but your request should be sent to the proxy after that.

Here is a an example of what I got working for myself:

var httpProxy = require('http-proxy');
//
// Http Proxy Server with bad target
//
var proxy = httpProxy.createServer({
  target:'http://localhost:9005' 
});

proxy.listen(8005);

var http = require('http');
//
// Create your target server
//
http.createServer(function (req, res) {
  var options = {
    target: 'http://'+req.headers.host
  };
  console.log(req.url)
  console.log(req.headers.host)
  req.host = req.headers.host;
  proxy.web(req, res, options, function(err){console.log('err', err)}); // errorCallback is optional
}).listen(9005);

proxy.on('proxyReq', function (proxyReq, req, res) {
  console.log('request url', JSON.stringify(req.url, true, 2));
});

It works only for http for now.

Sign up to request clarification or add additional context in comments.

7 Comments

This works, but how can I get the HTTP request urls? If I do console.log(req.url) then it logs: http://detectportal.firefox.com/success.txt http://google.com/ http://detectportal.firefox.com/success.txt http://detectportal.firefox.com/success.txt http://detectportal.firefox.com/success.txt and so on
I'm expecting to get: http://google.com/, ?gws_rd=cr&ei=ed4SWeCbNIKYsAHxmIvACw, googlelogo_color_120x44dp.png etc.
I think you need to see the documentation on request rewriting. here you should have access to the original request
For this one I should set the HTTP Proxy = 127.0.0.1 and Port = 5050 ? If I do this I'm getting The proxy server is refusing connections when trying to access a website...
You're right, but I was referring to the code from here github.com/nodejitsu/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.