Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a React Native App with Node js Backend on Android Device?

I want to run my React Native app on my local Android Device. The app uses Node js APIs for fetching data from mysql. The problem is, my android device can't access node js app which results in no data. The app works fine on Android Emulator on using address 10.0.2.2 but can't access this on Android Device P.S The node js APIs are o local server (localhost:3000)

like image 315
Faizan Khokhar Avatar asked Oct 16 '22 04:10

Faizan Khokhar


1 Answers

So I solved it like this. 1. I got my ip address from cmd using ipconfig 2. In node js, I wrote the server listening code like this

const hostname = "192.168.2.103";
const port = "3002";
app.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);
});

Now the app will run on this ip address instead of localhost and hence we can access it anywhere on the same network even in React Native fetching from APIs process

like image 121
Faizan Khokhar Avatar answered Oct 19 '22 00:10

Faizan Khokhar