Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling locally hosted server from Expo App

I am creating a react-native app and one of the components I have created contains a property which is populated through data coming from an http request.

Right now I am hosting the server from my laptop, however I am testing the app on my phone using the Expo app. Since these are two separate devices, the http://localhost:3000 calls are not working and thus I can not tell whether or not my component is properly working.

Is there any way for me to run the server on my laptop and set it up so that the http requests from within the Expo app reach the server?

like image 890
iSeeJay Avatar asked Nov 21 '17 16:11

iSeeJay


People also ask

How do you call a locally hosted server from Expo?

First you need to on Mobile HotSpot and connect to laptop using Mobile HotSpot. Then check you ip assign to your computer and replace api url http://localhost:80/ address to http://192.168.5.43:80/ in react-native source where you use. Replace port 80 to your api server port no.

How do you use tunnel Expo?

Expo runs the application in LAN by default and you can switch to Tunnel by simply clicking on it. Expo might prompt you to register to their platform and create an account if you haven't. This will take only a few minutes. After you have an account, switching yo tunnel mode should be enable for your application.

How do I run a Expo server?

To run a project's development server, view logs, open the app in a simulator, etc., during the development phase of an app. Logging into your Expo account. Publishing an app's JavaScript and other assets and managing to release them as updates. Managing Apple Credentials and Google Keystores.


2 Answers

You can get the IP address at runtime using the Expo manifest:

import Constants from "expo-constants"; const { manifest } = Constants;  const api = (typeof manifest.packagerOpts === `object`) && manifest.packagerOpts.dev   ? manifest.debuggerHost.split(`:`).shift().concat(`:3000`)   : `api.example.com`; 

This will set api constant to the address of your local development machine in development mode and to whatever address you use in production. Note that apps deployed through App Store / Play Store seems to have packagerOpts undefined. That's why we have additional typeof condition. In that case we assume it's production build.

More about the manifest here: https://docs.expo.io/versions/latest/workflow/how-expo-works/#expo-development-server

like image 164
Tad Lispy Avatar answered Sep 16 '22 15:09

Tad Lispy


import Constants from "expo-constants";  const { manifest } = Constants;  const uri = `http://${manifest.debuggerHost.split(':').shift()}:4000`; 
like image 36
tiagob Avatar answered Sep 19 '22 15:09

tiagob