如何在React Native/Expo中向Localhost提出请求?

发布于 2025-02-11 09:56:15 字数 1426 浏览 0 评论 0原文

我正在创建一个React Native/Expo应用程序,该应用程序使用 deno 在后端。我在后端创建了一个API,可以在Localhost:4000上本地运行。但是

[Unhandled promise rejection: TypeError: Network request failed] at node_modules/whatwg-fetch/dist/fetch.umd.js:535:17 in setTimeout$argument_0

import { Application } from "https://deno.land/x/oak/mod.ts";
import { oakCors } from "https://deno.land/x/cors/mod.ts";
import { APP_HOST, APP_PORT } from "./config.ts";
import router from "./routes.ts";
import _404 from "./controllers/404.ts";
import errorHandler from "./controllers/errorHandler.ts";

const app = new Application();

app.use(oakCors());
app.use(errorHandler);
app.use(router.routes());
app.use(router.allowedMethods());
app.use(_404);

console.log(`Listening on port:${APP_PORT}...`);

const App = () => {
  const getData = async () => {
    const response = await fetch("http://localhost:4000/something");
    const data = await response.json();
    console.log(data);
  };

  useEffect(() => {
    getData();
  }, []);

  return (
    ... 
  );
};

  • ​stackoverflow建议获取http:// [ip_address]:4000/something而不是localhost。我尝试过没有运气。
  • 我已经证实了API正在起作用。我可以在Vscode的Thunder Client中成功地称其为“ http:// localhost:4000”中的数据,在VSCODE的Thunder Client中成功地称为数据。

I'm creating a React Native/Expo app that uses Deno on the backend. I've created an API in the backend that I can run locally on localhost:4000. When I try to use fetch to call the API in the Expo app, however, I keep getting an error

[Unhandled promise rejection: TypeError: Network request failed] at node_modules/whatwg-fetch/dist/fetch.umd.js:535:17 in setTimeout$argument_0

Here is how I set up the backend

import { Application } from "https://deno.land/x/oak/mod.ts";
import { oakCors } from "https://deno.land/x/cors/mod.ts";
import { APP_HOST, APP_PORT } from "./config.ts";
import router from "./routes.ts";
import _404 from "./controllers/404.ts";
import errorHandler from "./controllers/errorHandler.ts";

const app = new Application();

app.use(oakCors());
app.use(errorHandler);
app.use(router.routes());
app.use(router.allowedMethods());
app.use(_404);

console.log(`Listening on port:${APP_PORT}...`);

And how I use fetch to call the API

const App = () => {
  const getData = async () => {
    const response = await fetch("http://localhost:4000/something");
    const data = await response.json();
    console.log(data);
  };

  useEffect(() => {
    getData();
  }, []);

  return (
    ... 
  );
};

Note

  • Some answers on StackOverflow suggest fetching http://[IP_ADDRESS]:4000/something instead of localhost. I've tried that with no luck.
  • I've verified that the API is working. I can call it successfully in VSCode's Thunder Client and I can also see the data by going to http://localhost:4000 in the browser.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纸短情长 2025-02-18 09:56:15

我找到了这个问题的解决方案。当我的服务器在计算机上运行Localhost时,我正在在物理设备上运行我的Expo应用程序。我无法在设备上向Localhost提出请求,因为Localhost没有在那里运行。

我通过使用

I found a solution to this issue. I'm running my Expo app on a physical device while my server is running on my computer, on localhost. It makes sense that I'm unable to make requests to localhost on my device, because localhost is not running there.

I fixed this issue by using ngrok, a tool that forwards localhost to a cloud URL, and fetching that URL in the app.

天涯沦落人 2025-02-18 09:56:15

使用本地IP分配到您的设备,例如: http://192.168.20.20.109:port/api/api/api/x < /a>

使用Windows或Linux中的IFConfig中的命令“ IPConfig”查找本地IP

Use the local IP assign to your device, like: http://192.168.20.109:port/api/x

find the local IP using the command "ipconfig" in windows or ifconfig in linux

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文