Search code examples
javascriptreact-nativeexpowampserver

Call local domain from React Native and expo


I have local website, with Wampserver and Windows (C:\Windows\System32\drivers\etc\hosts edited).

http://myproject.local

This site works great from my computer.

Now, I want create mobile application with React Native and Expo.

But, in my mobile app, when I call my website :

export function getForumIndex () {
    const url = 'http://myproject.local/api/test'

    return fetch(url)
        .then((response) => response.json())
        .catch((error) => console.error(error))
}

I have this error :

[21:29:58] JSON Parse error: Unrecognized token '<' node_modules\promise\setimmediate\core.js:37:14 in tryCallOne node_modules\promise\setimmediate\core.js:123:25 in unknown ... 8 more stack frames from framework internals

I think my phone and expo can not call the API on my computer (http://myproject.local/api/test). I am a beginner in mobile application. When I test with TMDb API it works well.

I do not know how to do it, can you help me ? :)


Solution

  • Since your application is running on your mobile phone, you need to connect to your computer over the network. The best way to do this is to connect both your phone and computer to the same wifi network, then replace myproject.local with the local IP address your computer has for the wifi network it's connected to e.g http://192.168.1.1/api/test where 192.168.1.1 is replaced by the local IP address for your computer. You can follow these steps for finding your local IP address.