GitHub user breautek added a comment to the discussion: How to get fetch 
TOWARDS localhost to FUNCTION on CARDOVA Android

`http://localhost:3000`

This domain will be the local device itself, so unless if your android 
emulator/device is running a web server on port 3000, this won't work.

I'm going to assume that you're not running the web server on the android 
device, that web server running port 3000 is running on your own workstation.

With that assumption... there are two options, one is only valid for android 
emulators. The other will work on both emulators and physical devices, for as 
long as they are connected to the same network (connected to the same 
wifi/router).

#### Emulator Only solution

The android emulators has a special 
[network](https://developer.android.com/studio/run/emulator-networking#networkaddresses)
 interface that refers to the host machine. IP `10.0.2.2` refers to your 
workstation's loopback interface, `127.0.0.1`. That means things you are 
running on your workstation and listening on `127.0.0.1` (aka `localhost`) 
should be accessible inside the android emulator through `10.0.2.2`

So:

```js
const response = await fetch('http://10.0.2.2:3000/poc3/convert', {
  method: 'POST',
  body: formData,
  headers: { 'Accept': 'audio/mpeg' },
});
```

Should work inside android emulators, assuming that you have a webserver 
running on your workstation on port 3000.

#### Private network solution

Private home networks typically allows unfettered access to other devices on 
the network, so you can use your workstation private ip to connect to the 
server, you just need to know how to find the your private ip.

Each router is different but most routers by default operate on `192.168.0.x` 
or `192.168.2.x`.

On linux/mac you can use: `ifconfig` and look for an interface that has `inet 
192.168.x.y`. Example of output is below:

wlp0s20f3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        **inet 192.168.50.132**  netmask 255.255.255.0  broadcast 192.168.50.255
        inet6 fe80::e41c:cfb1:4da4:b6fd  prefixlen 64  scopeid 0x20<link>
        ether 1a:52:a0:de:31:da  txqueuelen 1000  (Ethernet)
        RX packets 2012183  bytes 2700389985 (2.5 GiB)
        RX errors 0  dropped 2  overruns 0  frame 0
        TX packets 1044154  bytes 131777077 (125.6 MiB)
        TX errors 0  dropped 40 overruns 0  carrier 0  collisions 0

On windows the equivalent command is `ipconfig`, which will show something like:

Ethernet adapter Ethernet:

   Connection-specific DNS Suffix  . : home.local
   IPv4 Address. . . . . . . . . . . : **192.168.50.132**
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.50.1

Once you've found your private ip, you can use that to connect to your server 
over your home network:

```js
// 192.168.50.132 is following the example, but you'll need to replace it with 
your own ip as shown in ipconfig / ifconfig
const response = await fetch('http://192.168.50.132:3000/poc3/convert', {
  method: 'POST',
  body: formData,
  headers: { 'Accept': 'audio/mpeg' },
});
```

Couple of caveats with this approach:

1. many servers by default bind to 127.0.0.1, you may need to adjust settings 
to allow it to bind to your private ip, or `0.0.0.0` (to bind on all 
interfaces), so that you can accept connections over your private ip.
2. Routers by default use DHCP assignments, this means your private IP will 
change over time. Most routers however provides the ability to assign static 
ips to your devices.

I know networking can be confusing and it's generally not a skill set many 
developers have, it's more of a tertiary skill set. So I hope above helps, if 
not I can try to clarify anything that isn't clear.

GitHub link: 
https://github.com/apache/cordova/discussions/536#discussioncomment-12798462

----
This is an automatically sent email for issues@cordova.apache.org.
To unsubscribe, please send an email to: issues-unsubscr...@cordova.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@cordova.apache.org
For additional commands, e-mail: issues-h...@cordova.apache.org

Reply via email to