When I tried to debug 504 errors from specific servers, I found that changing this code
request(url, function (error, response, body) { if (error) { callback(error); } else { if (response.statusCode == 200) { callback(null, body); } else { // response.connection.remoteAddress is undefined here callback(new Error("Non status 200: " + response.statusCode + " received from " + response.connection.remoteAddress + " when requesting " + url)); } } });
to this code
request(url, function (error, response, body) { if (error) { callback(error); } else { if (response.statusCode == 200) { callback(null, body); } else { // response.connection.remoteAddress is now magically set somehow callback(new Error("Non status 200: " + response.statusCode + " received from " + response.connection.remoteAddress + " when requesting " + url)); } } }).on('response', function(response) { // This next line is very important. It seems to make the remoteAddress variable stay in the socket so we can read it. let remoteAddress = response.connection.remoteAddress; });
seemed to work. I have no idea why accessing the remoteAddress variable in the response handler forces it to remain in place for reading.
source share