1

I'm attempting to develop a program that communicates between devices over the HTTP protocol. If I hardcode the active IP address of the current device (i.e., the local IPv4 address that the network's router has assigned the device), I can accomplish this without issue.

However, I would like the program to determine the local, active IPv4 address automatically, so the code will run properly without modifications on any device, regardless of local IPv4 address.

I've tried using os.networkInterfaces(), but this returns an array containing all of the adapters available to the device. These adapters have differing keys, depending on the OS, device, language, etc., which makes determining the active local IP address difficult.

I've also tried using ip.address() from the NPM "ip" module, but this returns the wrong IP address in some instances. On my desktop (connected via ethernet), it returns the correct local IP (in the 10.x.x.x range). However, on my laptop (connected via WiFi), it returns the IP address of the ethernet adapter (in the 192.168.x.x range)—even when that adapter is not in use.

Both devices should return their IPs in the 10.x.x.x range, in my case, since my router assigns IPs in this range.

When I host servers on both my desktop and laptop at the same time, and then use the NPM "local-devices" module to find local devices, both my desktop and laptop only find IPs in the 10.x.x.x range, which means my laptop can identify and ping my desktop, but my desktop cannot identify and ping my laptop (since the laptop's server is running in the 192.168.x.x range, since this is the IP returned by ip.address()).

So my question boils down to this: how do I determine the current ACTIVE IP address of a device in Node.js? For example, in the case of my laptop, I want to determine the IP address for the WiFi adapter when connected to the network via WiFi (which is in the 10.x.x.x range).

3
  • 2
    Many systems have multiple addresses (at least 2 if you include the localhost address 127.0.0.1, and an interface can have both IPv4 and IPv6 addresses). You can get the local address of a particular connection, but it doesn't make sense to ask for the system's local address. Commented Sep 2, 2024 at 21:40
  • 1
    My PC has two ethernet interfaces, one with VLAN subinterfaces, and all, including the subinterfaces, have active IPv4 and IPv6 addresses. Each network to which it is connected has at least one router, but none of the routers assigned any of the addresses. Which address is the active address? They all are! The question really shows a lack of understanding for networking. Commented Sep 3, 2024 at 0:48
  • @RonMaupin There's a reason I'm not in networking or cybersecurity I suppose, haha. I probably would have phrased it as "how do I listen on all available addresses?" had I broken out of the "ooga booga, IP wrong" mindset. Commented Sep 3, 2024 at 1:08

1 Answer 1

-1

Nevermind, I just realized that you can omit the hostname when using HTTP.createServer().listen(), and Node will host the server on all available local IP addresses, which seems to fix the issue.

For more context, this was my .listen() previously:

// ...

server.listen(PORT, HOSTNAME, () => {
  console.log(`Server running at http://${HOSTNAME}:${PORT}/\n`);
});

...and this is it now:

// ...

server.listen(PORT, () => {
  console.log(`Server running on port ${PORT}\n`);
});
Sign up to request clarification or add additional context in comments.

Comments

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.