Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
-1
rated 0 times [  1] [ 2]  / answers: 1 / hits: 16243  / 12 Years ago, mon, june 11, 2012, 12:00:00

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


More From » http

 Answers
48

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);

[#84984] Sunday, June 10, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
joaquin

Total Points: 150
Total Questions: 103
Total Answers: 113

Location: Saint Helena
Member since Tue, Nov 3, 2020
4 Years ago
;