0

If I have a Node.js app running on a machine on the LAN that runs a web server (Fastify) and it is configured to be accessible to all devices on the LAN, how could I use JavaScript in the browser on another machine to detect that server on the LAN?

Example: On a NAS connected to my router, I run a Docker container that creates a HTTP API at the address 192.168.1.28:3000. I can manually type this into any browser on the LAN and it will access the Docker container. How can I use JavaScript to detect the existence of this server without knowing its address?

somebodysomewhere
  • 1,102
  • 1
  • 13
  • 25
  • Possible duplicate of https://stackoverflow.com/questions/3653065/get-local-ip-address-in-node-js – spirift Feb 09 '21 at 19:49
  • 1
    @spirift — The question is about announcing the server to the browser, not the server figuring out its own IP address (which *might* be a prerequisite for the solution, but doesn't address the meat of the problem). – Quentin Feb 09 '21 at 19:52
  • Sorry, didn't see the bit about the browser, just read about finding it using javascript – spirift Feb 09 '21 at 20:48

2 Answers2

2

You can't.

While there are protocols for announcing the presence of a service on a network, none are supported by JS running in a webpage.

Browser extensions (such as Bonjour Browser and the poorly reviewed Railduino Zeroconf-Lookup) are possible, but since you didn't mention writing a browser extension I'll assume they aren't an option.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

There's things like WebRTC but unless your device advertises that you're out of luck.

This is by design.

Can you imagine if an arbitrary web page could scan your local network and report back to some arbitrary server what it discovers?

tadman
  • 208,517
  • 23
  • 234
  • 262