0

I had a problem with running react-native app [Network Error] and the same is discussed here.

ngrok works for a temporary.

At the end of the day when I close my ngrok the token(link) generated is of no use. The next day if I have to run my project I have to regenerate a new token(link) and make the changes everywhere. Also, it's like running 3 ports for a single project.

Is there any solution for the same.

Thank You!!

Satyam
  • 567
  • 1
  • 6
  • 20
  • Is that problem for both iOS and Android? Also how are you making the HTTP requests - share your code. I have been able to connect to a localhost API in the past, so it's definitely possible and ngrok is not required. – Tsvetan Ganev Jun 25 '20 at 07:01
  • @TsvetanGanev I am running it on android. I am using Axios for HTTP requests. My code for front end and back end is described [here](https://stackoverflow.com/questions/62550990/network-error-react-native-express-nodejs-mongodb). If you need it in detail I'll post it here. – Satyam Jun 25 '20 at 07:10
  • 1
    I think your problem is similar to this one: https://stackoverflow.com/a/55827042/8597510 - you have to whitelist localhost to allow non-HTTPS traffic from your app – Tsvetan Ganev Jun 27 '20 at 08:45

1 Answers1

0

Finally, after very long research I got a solution to this problem.

But for now, it only works for an android emulator. I am not sure that this will work on a physical device using ADB.

Solution....

I replaced the http://localhost:3000 with http://10.0.2.2:3000/. You can also try this

import { Platform } from 'react-native';

export const baseUrl = Platform.OS === 'android' ?
    'http://10.0.2.2:3000/'
: 
'http://localhost:3000/';

This didn't work for me!!

Anyway, If there is a solution for the physical device(ADB) please I would love to know.

Thank you!!

Adios!!

Satyam
  • 567
  • 1
  • 6
  • 20