I simply use "fetch" to send GET request to my localhost API but it shows network fail. It works to send GET request to other non localhost website.
I would like to know the way to access my localhost API. the environment is that I use NativeBase and run it by Expo on android emulator. and the Expo connection is LAN.
Thank you.
you must use the ip instead of localhost:
for windows: ipconfig (IP ADDRESS) for mac: networksetup -getinfo Wi-Fi (IP ADDRESS)
the request would be like that:
axios.get('http://192.168.0.34')
I recommend using axios for react native
You can get an IP address at runtime using expo-constants module's manifest.
If you did not install this module, Run expo install expo-constants
import Constants from 'expo-constants';
const { manifest } = Constants;
...
const uri = `http://${manifest.debuggerHost
.split(`:`)
.shift()
.concat(`:1000`)}`; // Switch to the port you use. ex)localhost:3000 => .concat(`:3000`)}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With