I don’t understand if you are looking for information from the target server or the local server.
If you are searching for the port used by the proxy to connect to the target server, the best way is actually using the method res.connection._getpeername()
If you are searching for the port used by the client to connect to the proxy, you could use the option xfwd
of own httpProxy
to send the header x-forwarded
to the target server.
Thus, when the target server responds, you will have in the object of the request, the information.
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer({
xfwd: true
});
var http = require('http');
var server = http.createServer(function(req, res) {
proxy.web(req, res, { target: 'http://google.com/' });
});
server.listen(80);
proxy.on('proxyRes', function (res) {
var header = res.req._headers;
console.log(res.req.connection._getpeername());
console.log(header['x-forwarded-port']);
});
I used res.connection. _getpeername()
– leogaldioli