ChatGPT解决这个技术问题 Extra ChatGPT

How to allow access outside localhost

How can I allow access outside the localhost at Angular2? I can navigate at localhost:3030/panel easily but I can not navigate when I write my IP such as 10.123.14.12:3030/panel/.

Could you please allow me how to fix it? I am not using npm (node project manage - node install/node start ) to install and run the project.

If you want, I can provide my package.json and index.html.

do you use ng serve?
yes i am using ng server
did you try ng serve --host 10.123.14.12?
I tired ng serve and it says " I have to be inside an angular--cli project in ordre to use the serve command".

N
Nicu

Using ng serve --host 0.0.0.0 will allow you to connect to the ng serve using your ip instead of localhost.

EDIT

In newer versions of the cli, you have to provide your local ip address instead

EDIT 2

In newer versions of the cli (I think v5 and up) you can use 0.0.0.0 as the ip again to host it for anyone on your network to talk to.

As a side note Make sure your connection is set to Public in your OS settings.


it says "you have to be inside an angluar--cli project in order to use the serve command " with the red warning when i write ng sreve and also a yellow message which indicated that i am running version 6.2.2 of Node, which will not be supported in future versions of the CLI after writing ng serve
first, try updating node, second, try restarting your terminal/vscode
I updated it from package json to 6.0.46 as "@types/node": "6.0.46", but still get the same message
I found the answer myself. I was supposed to add ""server": "webpack-dev-server --inline --progress --host 0.0.0.0 --port 3000"," to packagejson
This worked for me: ng serve --open --host 0.0.0.0 --disable-host-check
A
Adiii

run command

ng serve --host=0.0.0.0 --disable-host-check

this will disable host check and allow to access from outside(instead of localhost) with IP address


I didn't need --disable-host-check. ng serve --host 0.0.0.0 works fine for me. What is the host check supposed to do?
I for once needed the disabled-host-check otherwise I got the message "Invalid Host header"
is there a way to define it in a config file to avoid mentioning it everytime?
@AyushKumar you can just define it in you package.json like { "scripts": { "start": "ng serve --host=0.0.0.0 --disable-host-check" } }
Excellent works for me.
S
Sajad

Mac users:

Go to System Preferences -> Network -> Wi-Fi Copy the IP address below Status (Usually 192.168.1.x) Paste it in your ng serve like: ng serve --host 192.168.1.x

Then you must be able to see your page on other devices through 192.168.1.x:4200.


Used to work for me and not working anymore. Wondering why. I feel like I've tried everything! My firewall is disabled. ng serve --host 10.x.x.x --disable-host-check / ng serve --host 0.0.0.0 --disable-host-check -> Not accessible from a Windows remote machine which is on the same network with 10.x.x.x:4200
Works well in Angular 7, and MacOS 10.14.6
For what it's worth, the accepted answer is working for me on MacOS as of October 2019.
This works for me, and after searching far and wide, it is the only answer I've seen to specify this key step: in the address bar of your iphone safari browser, plug in: 192.168.1.x:4200 , where x = final digits of your ip address. Including the ":4200" was the difference for me.
@RafaëlMoser did you solve it ? I have the same issue and I'm cluless really.
I
Irshad

You can use the following command to access with your ip.

ng serve --host 0.0.0.0 --disable-host-check

If you are using npm and want to avoid running the command every time, we can add the following line to the package.json file in the scripts section.

"scripts": {
    ...
    "start": "ng serve --host 0.0.0.0 --disable-host-check"
    ...
}

Then you can run you app using the below command to be accessed from the other system in the same network.

npm start

It's now -disableHostCheck=true|false in my version, Angular 8.
this is working if you are not using ng serve
r
rhavelka

I just edit angular.json file in my project as below and it works

...

    "serve": {
    "builder": "@angular-devkit/build-angular:dev-server",
    "options": {
      "browserTarget": "project:build",
      "host": "0.0.0.0"
    },
...

T
TSR

https://i.stack.imgur.com/JEhEj.png


I was not able to access from the same network even after served with ng serve --host 0.0.0.0 . But firewall permission was the issue as you mentioned. Thanks for taking to time to share this one.
v
vinzee

No package.json is necessary to change.

For me it works using:

ng serve --host=0.0.0.0 --port=5999 --disable-host-check

Host: http://localhost:5999/


A
Adiii

If you facing same problem inside Docker, then you can use below CMD in Dockerfile.

CMD ["ng", "serve", "--host", "0.0.0.0"]

Or complete Dockerfile

FROM node:alpine
WORKDIR app
RUN npm install -g @angular/cli
COPY . .
CMD ["ng", "serve", "--host", "0.0.0.0"]

R
Rakesh Ranjan

Open cmd and navigate to project location i.e. where you run npm install or ng serve for the project.

and then run the command - ng serve --host 10.202.32.45 where 10.202.32.45 is your IP address.

You will be able to access your page at 10.202.32.45:4200 where 4200 is your port number.

Note: If you serve your app using this command then you won't be able to access localhost:4200


Is it possible to access both way?
it works thanks..i guess i was looking at wrong ip address
know it sounds silly, but make sure your phone is using the same wifi network as you dev machine. I was using a guest network... +1 for the only answer that worked for me.
U
U12-Forward

A quick solution that might help someone :

Add this line as value for start ng serve --host 0.0.0.0 --disable-host-check in your package.json

ex:

{
"ng": "ng",
"start": "ng serve --host 0.0.0.0 --disable-host-check",
"build": "ng build",
}

And then simply do start or npm run start

you will be able to access your project from other local IPs.


o
otsili

ng serve --host 0.0.0.0 will allow you to connect to the ng serve using your ip instead of localhost. Open your browser to :4200 Using ng serve --host 0.0.0.0 --disable-host-check will allow you to connect to the ng serve using your domain instead of localhost. Open your browser to :4200


v
vinzee

In Angular version 11 (and maybe some earlier versions) --public-host option does the trick for me, for example:

$ ng serve --host 0.0.0.0 --public-host app.mypublicdomain.com

Z
Zombie

you can also introspect all HTTP traffic running over your tunnels using ngrok , then you can expose using ngrok http --host-header=rewrite 4200


P
Partho

Using ng serve --host 0.0.0.0 command has solved my issue. Use 192.168.x.x:5200 to get access to the app from another machine.

Also, check for firewall rules on client and server (disable firewall temporally or create a rule to allow traffic)


N
Nanda Kishore Allu

Use ng serve --host --port.

ng serve --host=192.111.1.11 --port=4545.

You can now see the below line at the end of compilation.

Angular Live Development Server is listening on 192.111.1.11:4545, open your browser on http://192.111.1.11:4545/ **.


J
Jithin U. Ahmed
ng serve --open --host 0.0.0.0 --disable-host-check

This worked for me perfectly (even using a public IP with an A host pointed to that IP).


d
double-beep

Create proxy.conf.json and paste this configuration

{  
"/api/*":
    {    
        "target": "http://localhost:7070/your api project name/",
        "secure": false,
        "pathRewrite": {"^/api" : ""}
    }
}

Replace:

let url = 'api/'+ your path;

Run from CLI:

ng serve  --host port.number —-proxy-config proxy.conf.json

Y
Yonas

for me it works using "ng serve --open --host 0.0.0.0" but there is a warning

WARNING: This is a simple server for use in testing or debugging Angular applications locally. It hasn't been reviewed for security issues.

Binding this server to an open connection can result in compromising your application or computer. Using a different host than the one passed to the "--host" flag might result in websocket connection issues. You might need to use "--disableHostCheck" if that's the case.


B
BossmanT

For the people who are using node project manager, also this line adding to package.json will be enough. For angular CLI users, mast3rd3mon's answer is true.

You can add

"server": "webpack-dev-server --inline --progress --host 0.0.0.0 --port 3000"

to package.json