Search code examples
javascriptnode.jsexpressserverip

Find the private ip of an express server


I have a node express server running on Machine 1 as (0.0.0.0) and some client applications running on Machine 2, 3, and so on... All machines are connected to the same Wi-Fi

How can I get the private IP of Machine 1 (which is running the express server), so that I can directly start calling the server APIs from client applications?

Note : I am using electron js in both server and client


Solution

  • There are a couple possibilities.

    1. You can assign a local IP address to your server that does not change and thus is known. For example, on my LAN, I configure the auto-IP assignment to avoid 192.168.1.200 - 192.168.1.255 and thus I can manually assign those IP addresses to devices for which I want to have a constant and known IP address. Then, you can just refer to the "known" local IP address from your clients as in http://192.168.1.201/somePath.

    2. You can configure local DNS (probably in your gateway router) so you can access your private server from your private LAN using a DNS name and combine this with option 1 so the IP address of your server won't change. You will need a router with this capability.

    3. You can use a resource discovery service such as Simple Service Discovery Protocol (built into Windows) or BonJour (built into MacOS, available for Windows) if your client has the capability to use these. Your server will advertise its services in the resource discovery system and the clients can query the system to find the server.