Reputation: 11
I have a Node.js server, the client is a mobile app. I want to know which would be the best way to know the local ip of the server. I mean...
Server runs in localhost:8080 and the client has to connect to the server like this 192.168.0.107:8080 (pc's local ip).
I want to guest this IP, 192.168.0.107 without writing it in client code because it could change.
Upvotes: 0
Views: 1671
Reputation: 618
I got your question as this, Your client have a private & dynamic IP because it is behind some NAT( i.e firewall), mobile app can request some STUN server to get its public IP and Port, then mobile app can send these data to server to identify the client. Similar to peer-to-peer connection establishment.
Upvotes: 0
Reputation: 4991
My guess is that you have a server with a dynamic ip, you want to connect your client to this IP without hardcoding that to the client's code. I'd say have many options:
Upvotes: 2
Reputation: 707148
Depending upon the network topology details of your actual implementation (which aren't included in your question), you have at least three different options:
Server Directly on Public Interent
On a public network (e.g the open internet), DNS is the normal answer to this type of question. You follow the following steps:
Make sure you have a public IP address. As you may or may not know 192.168.0.107
is not a public IP address. It is a local internet address and cannot be routed on the open, public internet.
With a DNS service, create a DNS record that establishes a link between a host name such as "google.com" and your public IP address.
Code the client to connect to your "name", not your IP address.
Server and Mobile App Only on Private Network
If your mobile app runs only on your private network (perhaps via WiFi), then you can configure your own DNS server on your private network that will do essentially the same thing as the steps above, but will work for a private address like 192.168.0.107
on your private network.
Server Lives on Home Network, Want to Access From Internet
If your server lives in your private network (and thus only has a private IP address), but you want to be able to reach it from the public network, then you need to configure some sort of routing or port forwarding at the boundary between your private network and your public network. In a home router situation, this would involve the following steps:
Upvotes: 0
Reputation: 150614
IMHO this is not possible in a really reliable way. There are several reasons for this, including:
So either you need to narrow down what you want to achieve or you have to live with some kind of uncertainness.
The easiest way then would be to do a DNS lookup, using the hostname, to get the well-known IP addresses, and then select one of them (however you want to do this, that's up to you).
PS: I'm not perfectly sure that I really got your question. If I understood you correctly, you want to find out the IP address of the client on the client, because you need to send it to the server, right?
Upvotes: 1
Reputation: 501
If you are looking for a simple answer, there is a npm module that does this for you, it's really small so just install it and use the instructions to get the ip of the client:
https://www.npmjs.com/package/request-ip
Getting Started:
var requestIp = require('request-ip');
// inside middleware handler
var ipMiddleware = function(req, res, next) {
var clientIp = requestIp.getClientIp(req); // on localhost > 127.0.0.1
next();
};
Upvotes: 0