Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test an app created with Angular CLI ng serve from another device?

I have an app generated with Angular CLI from scratch. CLI version angular-cli: 1.0.0-beta.11-webpack.2

I am trying to test it from my smartphone but I get Connection refused.

So, I run ng serve on my laptop and try to access the app:

  • From laptop, using localhost: Works
  • From laptop, using IP: Connection refused
  • From smartphone, using IP: Connection refused

This used to work with the previous, SystemJS version of CLI. I checked that I don't have a firewall running.

How could I fix or debug this error?

I am using a Mac.

like image 677
Carlos Mermingas Avatar asked Aug 21 '16 23:08

Carlos Mermingas


People also ask

Can angular app run on mobile?

Angular is an excellent tool for building any kind of mobile app. Naturally, you can use this framework to build a web application that runs on any device. However, you can combine Angular with NativeScript, another open-source framework that allows building iOS and Android.

What angular CLI command builds and serves your app?

ng servelink. Builds and serves your application, rebuilding on file changes.


2 Answers

Adding the host-flag with value "0.0.0.0" should allow you to access the webserver from any device on your local network.

This should work: ng serve --host 0.0.0.0

For an explanation: https://github.com/angular/angular-cli/pull/1475#issuecomment-235986121

like image 96
grotz Avatar answered Sep 29 '22 01:09

grotz


In package.json

 "start": "ng serve --host 0.0.0.0   --port 4200 --disable-host-check ", 

However --disable-host-check would be a security risk and you will need "@angular/cli": "^1.1.0-rc.2" as this flag appeared in 1.1 version

like image 27
bravik Avatar answered Sep 29 '22 01:09

bravik