Visiting localhost from a physical Android device
I recently had to test a web app I was developing on an real mobile phone, so I had to figure out how to (1) run the development server on my computer, and (2) access it from the Android phone I had in my hand.
Most of the how-tos I found online were either outdated, or felt like they were a bit too complicated for what I was trying to achieve. I did not want to have to switch my computer network every time I needed to test something on my phone; just thinking about having to switch back and forth between WiFi and a mobile hotspot tires me out.
I'd rather have a solution that I set up once, and then just use it from then on out.
So how then?
You might already have adb
installed on your computer, but if you don't, you can install it from the Android SDK Platform Tools site.
Next, you need to enable USB debugging on your Android device. This lets you communicate to your phone from your computer over USB. The steps to do that can be found here.
After all that is done, you're ready to go. Connect your phone to your computer via USB --- you can optionally check if your phone is visible to your computer by running the following command:
adb devices
If you see your device listed, you're good to go.
Finally, whenever you want to access your localhost server from your phone, you just need to run the following command:
adb reverse tcp:8080 tcp:8080
This assumes that your development server is running on port 8080
. Now, as long as your phone is connected to your computer via USB, you can use any browser on your phone to access http://localhost:8080
and it will be forwarded to your computer's localhost server.