简体   繁体   中英

http://localhost:4200 from windows is working with ng serve from WSL2, how come?

When I run "ng serve" from WSL2 (Ubuntu 20.04), I am able to access the angular application from Windows 10 Host by browsing to http://localhost:4200.

Can someone explain how this is possible? I understand that WSL2 is working as a virtual machine with its own ip address. How can Windows 10 host access the angular app on WSL2 using "http://localhost:4200" ?

On a different note, when I run tomcat from WSL2, I am NOT able to access the tomcat application from Windows using "http://localhost:8080".

Thanks in advance Paul

The assumption that WSL2 is working as a virtual machine with its own ip address depends on the Windows 10 version.

Also read this article . It explains why and how.

Accessing Linux networking apps from Windows (localhost)

If you are building a networking app (for example an app running on a NodeJS or SQL server) in your Linux distribution, you can access it from a Windows app (like your Edge or Chrome internet browser) using localhost (just like you normally would).

However, if you are running an older version of Windows (Build 18945 or less), you will need to get the IP address of the Linux host VM (or update to the latest Windows version).

I had a similar problem: The default React server worked fine, but the Tomcat server wouldn't even if they were running on the same port. Dudi Boy's answer suggests using a specific IP on older WSL versions, but that didn't help me much because I'm on a newer version. However, his link contained another useful piece of advice

Connecting via remote IP addresses

When using remote IP addresses to connect to your applications, they will be treated as connections from the Local Area Network (LAN). This means that you will need to make sure your application can accept LAN connections.

For example, you may need to bind your application to 0.0.0.0 instead of 127.0.0.1 . In the example of a Python app using Flask, this can be done with the command: app.run(host='0.0.0.0') . Please keep security in mind when making these changes as this will allow connections from your LAN.

Sure enough, when I had my server run on 0.0.0.0 instead of 127.0.0.1 , it worked!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM