How to get automatically local backend url in __DEV__?

Hi,

I’d like to avoid managing my api url with environment variables.

Formerly I was using this code in DEV which worked fine in Expo client:

const { hostname } = url.parse(NativeModules.SourceCode.scriptURL);
const backendUrl = http://${hostname}:10400;

However now I’ve detached and run with XCode / Android Studio, on emulators or device and the above code does not work in such scenarios, leading to a backend url like “http://packager.qv-rrd.slorber.photographers.exp.direct:10400

I was wondering if there was any cross-platform (android/ios/expo-client/emulators) that would permit me to easily get my api url (which runs in the same computer I run “exp start” on)

Thanks

Hi, try examining the contents of Expo.Constants.manifest, which will contain information about where the app is being served from.

Whether in ExpoKit or not, you can always exp start --localhost or exp start --lan to switch the kind of development url generated by your exp instance.

thanks I didn’t know about these cli options :slight_smile: will try that asap

Not sure it’s normal but I got this error with “exp start --lan” (but it’s still working fine, just unexpected error msg)

[exp] Error starting tunnel: XDLError: failed to start tunnel

(edit: it seems on second command run I don’t get this error now, maybe I made a mistake?)

Also worth mentionning I could not get --localhost option to work in Android simulator, in which usecase is it supposed to work? --lan option seems to work fine on phone/simulator (as long as phone is on same wifi network) so I wonder why I would use localhost instead of always using --lan?

hey @slorber, --localhost will only work in the android simulator if you run adb reverse first

thanks, I’ll stick to --lan as a default as it seems to be the thing that works most easily in all the situations I want :wink: