javascript - 我尝试将我的 Expo 应用程序连接到 localhost 后端,但它不起作用
问题描述
我的服务器在 localhost:5000 上运行,我的 iPhone 使用 Expo 使用隧道方法连接到 Metro 服务器。我试图用我的电脑的 IP 地址更改 localhost,但日志如下:
Network request failed
at node_modules\whatwg-fetch\dist\fetch.umd.js:535:17 in setTimeout$argument_0
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:130:14 in _callTimer
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:383:16 in callTimers
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:416:4 in __callFunction
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:109:6 in __guard$argument_0
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:108:4 in callFunctionReturnFlushedQueue
at [native code]:null in callFunctionReturnFlushedQueue
我的 fetch 函数的代码是:
const onSubmitHandler = () => {
const payload = {
phone,
name,
surname,
password,
city,
dateOfBirth,
};
fetch("http://192.168.1.40:5000/signup", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
})
[....]
}
} catch (err) {
console.log(err);
};
})
.catch(err => {
console.log(err);
});
};
我的服务器 index.js 是:
import express from 'express';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
import sequelize from './utils/database.js';
import router from './routes/routes.js';
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use((_, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
});
app.use(router);
sequelize.sync();
app.listen(5000);
我该如何解决这个问题?
解决方案
推荐阅读
- clojure - 不同命名空间中的 Clojurescript defprotocol 和 defrecord
- mips - 我在 MIPS 中写了一个循环,但由于某种原因它一直在无限运行?
- react-native - 不变违规:[2365,"RCTView",11,{"position":"absolute","width":"<
>","height":4 不能用作本机方法参数 - android-studio - 如何在 Android Studio 中重新启动线程?
- javascript - setTimeout 没有按预期工作 React Native
- java - 使用 Python 连接到 AWS ActiveMQ 代理
- node.js - 如何使用 jest-mock-axios 来测试调用 axios.get 的函数?
- google-cloud-platform - 即使谷歌云中的磁盘已满,快照大小是否小于实际磁盘大小?
- xcode - 无法安装 Watch 应用程序 - WatchKit 1.0 应用程序不再可安装在此 watchOS 版本上
- c# - 根据 ViewModel 条目呈现动态复选框列表的最佳实践