2

How can I get the global IP of the server?

I could do a http request to 'http://wtfismyip.com/json' and get the IP address.

Is there a way to get the IP (or dns) whitout doing an outside request? I can't find anything on the 'process.env' or 'os' object.

I use nodejs with express and deploy to heroku.

Razor
  • 635
  • 7
  • 17
  • I'm reasonably sure that anytime you ping a server, it must resolve to the IP address in order to do so. To that effect, this link might help: http://stackoverflow.com/questions/4282151/is-it-possible-to-ping-a-server-from-javascript – durbnpoisn Aug 15 '14 at 17:47
  • What specifically are you needing to do here? Making an outbound request to that URL may not even be correct, as that will return the public source IP your traffic is generated from, *which may differ from the IP assigned for **inbound** traffic to your server*. – admdrew Aug 15 '14 at 17:56
  • Well I need the IP to put it in a request header. Some servers don't respond if I don't have the correct headers.host set. – Razor Aug 15 '14 at 18:05
  • Yes, the public source IP is what I need. – Razor Aug 15 '14 at 18:06
  • 1
    `the public source IP` Then you will need to access an outside resource; the upstream routers/firewalls from your server perform the address translation on your outbound traffic, so you will need to connect to a resource somewhere beyond that translation. – admdrew Aug 15 '14 at 18:13
  • I guess you are right. There is no point for the Node server to know the outside ip. It only has the information from the local server it's running on. Like the hostname. The IP comes from the router/server that makes the NAT, as you say. I'll have to communicate with that server/router to get the outside IP, this could happen only if I could use a heroku addon or something, don't really know how exactly. – Razor Aug 15 '14 at 18:21
  • Considering that I don't find any information about getting the global IP from heroku, I will do the http request to a 'whatismyip' address. Of course I could use the domain name of my webbapp, this is static, but I wanted to automate this in case I deploy to a different domain. – Razor Aug 15 '14 at 18:25

1 Answers1

1

Simple answer: No. You'll need some kind of outside source. That source could be as local as the router if you had access to it, but it's more work than it's worth. And if you're on a hosting solution that's out anyway.

I assume your reason for wanting to get this quickly is for speed optimization? If so then I recommend you not worry about it. It's such a small action it won't have any lasting impact.

Now that I think about it; you should already know your public IP! If it's a hosted solution it ought not to change so you can get it once and just hard-code it into your project. I have no experience with heroku though, so it's possible they use a dynamic IP.

OneHoopyFrood
  • 3,611
  • 2
  • 19
  • 37
  • Yes, you are right. I wanted to automate this in case I deploy to other domains. Thought heroku had an addon or something for this. Can't find anything in the documentation. – Razor Aug 15 '14 at 18:31