How to run my Angular site on mobile device that is running localhost on my windows desktop

AndroidNetworkingLocalhostWebserverAngular Cli

Android 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.

  1. ng serve --host 0.0.0.0
  2. Find your local IP address by following this
  3. 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

  1. 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.

  1. Next, type ng serve --host 192.168.0.10.

  2. Ok, your app on air for all devices on same network.

  3. 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:

  1. Settings
  2. Wifi
  3. Long Click your desired Network
  4. Modify Network
  5. Click advanced/extended Options
  6. Set proxyHostname: localhost and proxyPort: 4200 and bypassProxyFor: 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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionH. PauwelynView Question on Stackoverflow
Solution 1 - AndroidAhmed MusallamView Answer on Stackoverflow
Solution 2 - AndroidShabbir DhangotView Answer on Stackoverflow
Solution 3 - AndroidRam SharmaView Answer on Stackoverflow
Solution 4 - AndroidbensenView Answer on Stackoverflow
Solution 5 - AndroidKaloyan StamatovView Answer on Stackoverflow
Solution 6 - AndroidAhmed AlhisaieView Answer on Stackoverflow