I’m trying to set the development server so I can test the device interactions with my local PHP server. I’m trying to do this following some instruction I found (1, 2, 3). I always did this interaction with
ionic serve and never had a problem, but now I really need to test some exclusive device functions.
My computer local IP is: 10.0.0.101
PHP local server: localhost:1337
Device and computer sharing the same network
If I start my app normally with
ionic cordova run android --livereload and try to access the PHP server (localhost:1337) with a HTTP POST method firing to 10.0.0.101:1337, I can’t find the connection.
If I try to change the address with
ionic cordova run android --livereload --address=10.0.0.101:8100 I receive this error:
Error: getaddrinfo ENOTFOUND
I see that some people run with this same node problem, but trying to downgrade the
serve library doesn’t work for me.
What is the correct way to accomplish this? If there is any missing info let me know.
I do my best to avoid using
localhost, especially when doing mobile development. When running on device,
localhost is the device itself. I suggest always using hostnames and DNS.
I agree that using the localhost name can cause some confusion, especially for those who don’t know much about computer networks. Localhost is translated through DNS to 127.0.0.1, which is the device itself, just as you said.
Taking this into consideration and complementing the details of my problem, from my perspective the main method I’m using to make the connection should work if I can get past this error. This is because my local PHP server is set to 127.0.0.1:1337 (computer), which is connected to my local network as 10.0.0.101. My device is also on the same 10.0.0.x LAN as 10.0.0.103. If I understand correctly when I use the
--address:10.0.0.1:8100 attribute when running the app, the dev server should run in my computer and not in the device. Considering this, the HTTP POST connection method would be fired to 127.0.0.1:1337, which would be the in the same device that the local PHP server is deployed.
Note: If I try to ping in my computer to 10.0.0.3, all packages are delivered correctly.
Alternatively I could port forward to port 1337 so that I could access the database from device. However, if possible, I would like to resolve this issue without having to do so.
Updating my problem:
I realized that
--address option only accepts the address, without the port. So I ran that way:
ionic cordova run android --address=10.0.0.101 --port:8100
Now I’m receiving the Ionic message: “Project is running at http://10.0.0.101:8100/webpack-dev-server/”, which is a good sign as the dev server is running in the same device as my local PHP server and now I should be able to connect both. However the problem remains the same, I can’t connect to my local server (ERR_CONNECTION_REFUSED).
Apologies if this is either (a) old hat to you and/or (b) irrelevant, but the
bind system call takes an address, a host can have many addresses, and if you don’t try to connect to the right one, it will fail. There is a wildcard address of 0.0.0.0 that means “listen on all interfaces”, but if you’re not using it, then you have to care about what address things are listening on. Taking what you said literally,
That must be accessed via 127.0.0.1.
Red herring. Unless the server is listening on 10.0.0.101, it won’t accept connections to that address.
Ok, tried a different approach based on what you said. For debug and development I’m using PHP built in server and I was starting it this way:
php -S localhost:1337
In my APP, the HTTP request was firing to
10.0.0.101:1337, which was denied right away.
Now I started the PHP server this way:
php -S 10.0.0.101:1337 and changed the HTTP request to
10.0.0.101:1337 too. Now I’m not receiving the error imediatelly, but the request in pending for some time until I get the
This happened only in one machine, running the same method in other computer worked as expected. Thank you again for helping me with another problem!