Node.js 使用React Native连接到本地主机

Node.js 使用React Native连接到本地主机,node.js,react-native,express,Node.js,React Native,Express,我正在尝试使用react本机fetch get请求从本地托管的express API获取json。 我们的react本机代码是: useEffect(() => { fetch("http://localhost:5000/api/listings") .then((response) => response.json()) .then((responseJSON) => { console.log(resp

我正在尝试使用react本机fetch get请求从本地托管的express API获取json。 我们的react本机代码是:

 useEffect(() => {
    fetch("http://localhost:5000/api/listings")
      .then((response) => response.json())
      .then((responseJSON) => {
        console.log(responseJSON);
        setIsLoading(false);
        setListings(responseJSON);
      })
      .catch((error) => console.log(error));
  }, []);
尝试发送请求时记录以下错误:

Network request failed
at http://192.168.1.34:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:30140:19 in <unknown>
at http://192.168.1.34:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:31129:20 in <unknown>
at http://192.168.1.34:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:31045:8 in _callTimer
at http://192.168.1.34:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:31253:8 in Object.callTimers
at http://192.168.1.34:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:3213:30 in MessageQueue.__callFunction
at http://192.168.1.34:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:2945:16 in <unknown>
at http://192.168.1.34:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:3167:12 in MessageQueue.__guard
at http://192.168.1.34:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:2944:13 in MessageQueue.callFunctionReturnFlushedQueue
网络请求失败
在http://192.168.1.34:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:30140:19 在里面
在http://192.168.1.34:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:31129:20 在里面
在http://192.168.1.34:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:31045:8 in_callTimer
在http://192.168.1.34:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:31253:8 在Object.callTimers中
在http://192.168.1.34:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:3213:30 在MessageQueue.\uu调用函数中
在http://192.168.1.34:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:2945:16 在里面
在http://192.168.1.34:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:3167:12 在消息队列中
在http://192.168.1.34:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:2944:13 在MessageQueue.callFunctionReturnFlushedQueue中

当从postman发送get请求时,会显示json,因此我不知道出了什么问题。

开发环境是一个上下文,而android runner环境(如果是这样)是另一个上下文

如果后端位于节点中,则可以使用。它是如此独立,我认为这是最简单的形式


如果您使用的是Linux(类似Debian),您也可以在/etc/hosts/file中配置主机。

我相信下面的代码会对您有所帮助。在终端/cmd中输入它。你的模拟器必须是打开的

adb reverse tcp:5000 tcp:5000
现在你的链接应该可以工作了
http://localhost:5000/api/listings

如果第一个选项不起作用,请尝试用以下链接替换您的链接:

http://10.0.2.2:5000/api/listings


这是因为Android不将localhost理解为您的PC,因为它是localhost,所以在第一个选择中,我们重定向Windows/Linux的模拟器门流量。在MacOS中不会发生此错误,因为MacOS知道整个环境都是本地主机。

从其他设备发出的请求也是如此吗?在这种情况下,
localhost
指的是该设备的本地主机;不是您计算机的本地主机,您需要将本地主机更改为计算机的本地IP。@cbr成功了!非常感谢你。