2

I'm using node.js request module and I want to get the resolved IP address without using the dns module.

E.g.

var request = require('request');

request("http://example.com", function(e, r, body) {
  // how do I get example.com IP address here?
});

2 Answers 2

1

So apparently the response.connection.remoteAddress answer posted elsewhere was true, but the value was set to undefined when execution reached response handler. To work around this, I added an extra event listener to save the value:

request("http://example.com", function(e, r, body) {
  console.log("doesn't work", r.connection.remoteAddress);
  console.log('does work', r.remoteIP);
}).on('response', function(res) {
  res.remoteIP = res.connection.remoteAddress;
});
Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure if things have changed since you posted your answer, but in case anyone else ran across this thread, the following worked fine...

const request = require('request');

request("http://example.com", (e, r, body) => {
    console.log(r.connection.remoteAddress)
})

The output was: 93.184.216.34

I didn't have to add an event listener.

1 Comment

returns undefined for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.