With the simple solution (just access laptop_ip_addr:port
from mobile device, when mobile and laptop are on the same WiFi), I get a ERR_CONNECTION_REFUSED error. That is, my MacBook seems to refuse the connection attempt from my mobile.
ADB Reverse Socket (Android only)
This solution works for me (tested with a MacBook):
- Connect Android mobile device with USB cable to laptop
- Enable USB Debugging on mobile device
- On laptop, run
adb reverse tcp:4000 tcp:4000
- Use your custom port number instead of
4000
- Use your custom port number instead of
- Now, on the mobile device, you can navigate to
http://localhost:4000/
, and it will actually connect to the laptop, not the mobile device
See instructions here.
The downside is that this works only with a single mobile device at a time. If you want access with another mobile device, you have to first disconnect the first one (disable USB Debugging), connect the new one (enable USB Debugging), and run adb reverse tcp:4000 tcp:4000
again.
ngrok (works with all devices)
Another solution that should always work is ngrok (as mentioned in other answers). It works over the Internet, and not the local network.
It's extremely easy to use:
brew cask install ngrok
ngrok http 4000
This outputs, among some other information, a line like
Forwarding http://4cc5ac02.ngrok.io -> localhost:4000
Now, you can navigate to http://4cc5ac02.ngrok.io
on any device that is connected to the Internet, and this URL redirects to localhost:4000
of your laptop.
Note that as long as the ngrok command is running (until you hit Ctrl-C), your project is publicly served. Everybody who has the URL can see it.