如何在 Android 设备上运行带有 Node js 后端的 React Native 应用程序? [英] How to run a React Native App with Node js Backend on Android Device?

查看:27
本文介绍了如何在 Android 设备上运行带有 Node js 后端的 React Native 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的本地 Android 设备上运行我的 React Native 应用程序.该应用程序使用 Node js API 从 mysql 获取数据.问题是,我的 android 设备无法访问 node js 应用程序,导致没有数据.该应用程序在使用地址 10.0.2.2 的 Android Emulator 上运行良好,但无法在 Android 设备上访问此地址P.S 节点 js API 是本地服务器 (localhost:3000)

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)

推荐答案

所以我是这样解决的.1. 我使用 ipconfig 从 cmd 获得了我的 ip 地址2.在node js中,我写了这样的服务端监听代码

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}/`);
});

现在应用程序将在这个 IP 地址而不是本地主机上运行,​​因此我们可以在同一网络上的任何地方访问它,即使在 React Native 从 API 获取过程中

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

这篇关于如何在 Android 设备上运行带有 Node js 后端的 React Native 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆