How to run my Angular site on mobile device that is running localhost on my windows desktop
AndroidNetworkingLocalhostWebserverAngular CliAndroid Problem Overview
I know from this question and answer on Super User Site running on localhost on desktop is unreachable on android device (PS: this question continues on the question in the link) that the command ng serve
opens a bad configured webserver.
The question is how could I run my site on an Android device without deploying my site. I'll try to run the site on this url http://192.168.0.168:4200
using Angular-CLI.
Android Solutions
Solution 1 - Android
try ng serve --host
option as explained in this issue: https://github.com/angular/angular-cli/issues/1793 comment by intellix.
ng serve --host 0.0.0.0
- Find your local IP address by following this
- Navigate to
<local IP adress>:4200
from any device on the same network.
NOTE: If you get CONNECTION_REFUSED just disable your firewall and you should be good to go.
otherwise, try https://ngrok.com/ to open a tunnel from your local network to your computer's localhost. Basically your computer forwards the 4200 port to an external IP and port that any device on the network can access.
Solution 2 - Android
- First verify your machine IP Address.
- On Windows, type
ipconfig
in Prompt (copy IPv4). - On Mac, type
ifconfig |grep inet
in Terminal (copy inet).
In my case this is 192.168.0.10
.
-
Next, type
ng serve --host 192.168.0.10
. -
Ok, your app on air for all devices on same network.
-
Now only
http://192.168.0.10:4200/
will work,localhost
not more.
Solution 3 - Android
When i tried using
ng serve --host local_ip_address
I was facing
> Invalid header Request
This issue was solved using --disable-host-check
ng serve --host local_ip_address --disable-host-check
Solution 4 - Android
As an addition to all the answers above, you might need to adjust your proxy settings on your device. For my Android Phone, this was:
- Settings
- Wifi
- Long Click your desired Network
- Modify Network
- Click advanced/extended Options
- Set
proxyHostname: localhost
andproxyPort: 4200
andbypassProxyFor: hostIpAddress
Solution 5 - Android
I open my Angular2 App on Mobile device using Chrome Remote Device.
You just have to connect your phone to your pc and sync it with chrome. Than do the port forwarding as described at the link above.
After connect your device, run your Angular app serve with ng serve --public-host
Now you can access your app in localhost:4200 from mobile device.
Solution 6 - Android
for Ubuntu people:
get the ip address writing this in the terminal:
hostname -I
you will get an ip address something like 192.168.0.0
and then you can just serve the app with:
ng serve --host ip_address
and then you can just open the browser in your mobile with:
http://ip_address:4200