8

Have created a simple GET API using node.js and trying to consume it within my expo-react native project using axios/fetch.

The GET API is called whenever user clicks on submit button.

Submit -> http://localhost:port/api

However, when I am trying to test the feature on my android device by connecting the device through datacable and selecting "local" connect type in expo, the API call always falls under exception.

When I tried to log the exception, the exception is printed empty.

I double checked the API and it works fine when tested through postman.

I tried replacing localhost url with my-Ip (http://IP:port/api) and also with 127.0.0.1/127.0.2.2 (http://127.0.0.1:port/api , http://127.0.2.2:port/api) , but nothing seems to make the API call a success.

When I replaced the localhost URL with Facebook's network sample API, the application worked just fine. So, there is no issue with the API calling method.

Also, tried adding the port to windows inbound firewall request but that didn't work too.

Can someone help me out with this ?

3 Answers 3

4

You must call the API from your local IP address. To get your local IP, just do the following:

If you are on Ubuntu 16.04:

ifconfig | grep 192

If you are on Ubuntu 18.04:

ip -c a | grep 192

Your local IP address will probably be something like 192.168.0.XXX.

Sign up to request clarification or add additional context in comments.

8 Comments

Tried replacing localhost with my local IP, but sadly didn't work.
Make sure your phone is connected to the same wi-fi as your computer. And check if the local IP is accessible from the same computer. Sometimes there are problems regard ports and firewall.
The phone and the laptop are connected to the same WiFi .Tried accessing myIp:port/api on my mobile browser, it seems to be unreachable too.
For now ngrok is the way to go for me. Thanks vmf91 for sticking through.
Yes, with ngrok everything is working. Thanks for checking :).
|
3

yes, i also had this problem until use my Local IP (example: http://192.168.1.55:port) instead 127.0.0.1 please see this answer

1 Comment

Tried replacing localhost with my local IP, but sadly didn't work.
0

Good day, This is working

I use my machine's IP the Ethernet adapter Ethernet 2: IPV4 Address

And allow my port on firewall both Inbound and outbound rules

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.