6

I am trying to implement the simplest example:

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

httpProxy.createServer(function (req, res, proxy) {
    //
    // I would add logging here
    //
    proxy.proxyRequest(req, res, { host: 'www.google.com', port: 80 });
}).listen(18000);

When I configure my browser to use this proxy and I navigate to www.google.com I receive no response. What is that I am doing wrong?

I'm using Windows 7 Chrome

2
  • Can you specify what OS and what browser? Each handles proxies differently. Commented Jun 11, 2012 at 18:21
  • Is this working for you with the latest changes in the library? Seems like now it is mandatory to pass the target field in the options otherwise just simply running the above code gives me Must provide valid URL for target Commented Dec 15, 2017 at 18:50

3 Answers 3

6

Here is an simple example how to log requests. I use a similar to log all my domains to one database.

I copied much from http://blog.nodejitsu.com/http-proxy-middlewares (archived)

var fs = require('fs'),
    http = require('http'),
    httpProxy = require('http-proxy'),
        
logger = function() {    
  // This will only run once
  var logFile = fs.createWriteStream('./requests.log');

  return function (request, response, next) { 
    // This will run on each request.
    logFile.write(JSON.stringify(request.headers, true, 2));
    next();
  }
}

httpProxy.createServer(
  logger(), // <-- Here is all the magic
  {
    hostnameOnly: true,
    router: {
      'example1.com': '127.0.0.1:8001', // server on localhost:8001
      'example2.com': '127.0.0.1:8002'  // server 2 on localhost:8002
  }
}).listen(8000);
Sign up to request clarification or add additional context in comments.

2 Comments

Should this still be working? I'm trying to log the requests when I access different urls but can't figure it out...
This doesn't work anymore, as now it is mandatory to pass some kind of a target or a forward field in the options. Running the above code gives me Must provide valid url for Target.
1

I am not sure if this helps because the posted information are really short. But I found a post that they updated the api ...

you might want to check out this post:

Updating to node-http-proxy v0.5.0 http://blog.nodejitsu.com/updating-node-http-proxy

1 Comment

0

I done like logging the request headers object on the event proxyReq

const http = require('http'),
    httpProxy = require('http-proxy'),
    fs = require('fs');

const proxy = httpProxy.createProxyServer({});

const logFile = fs.createWriteStream('./requests.log');

proxy.on('proxyReq', function (proxyReq, req, res, options) {
    //Log incoming request headers
    logFile.write(JSON.stringify(req.headers, true, 2));
});

const server = http.createServer(function (req, res) {
    proxy.web(req, res, {
        changeOrigin: true,
        target: 'example1.com'
    });
});

console.log("listening on port 5050")
server.listen(5050);

Comments

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.