How to find the server port that answered the request

Asked

Viewed 702 times

4

I’m using the http-proxy to create a load Speak in Nodejs, this module has the event

proxy.on('proxyRes', function (res) {
  ..
});

Within this event, I would like to grab the server port that answered the request by the parameter res.

EDIT

While I Depurava executed the method res.connection.remoteAddress, in which after execution appears the property res.connection._peername Now I ask, this is the only and most correct form?

1 answer

1


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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.