I do not understand if you are looking for information from the target server or local server.
If you are looking for the port used by the proxy to connect to the target server, the best way is actually using the res.connection._getpeername()
If you are looking for the port used by the client to connect to the proxy, you could use the xfwd
option of the httpProxy
itself to send the header x-forwarded
to the target server.
So when the target server responds, you have the information in the request object.
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']);
});