Axios网络错误与React native, Nodejs, Express, MongoDB &



我正试图开发使应用程序与服务器。我有下面的设置。
React native Expo-cli
nodejs + express
mongodb

有一些麻烦连接客户端程序和服务器程序与物理android手机和ios手机。
我通过ssh登录linux服务器程序,它的ip地址和使用的端口如下
服务器ip: 102.249.18.175
端口:443

部分前端代码如下

try {
const response = await axios.post(
"http://192.249.18.175:443/userAuth/signin",
{
email,
password,
}
);
console.log("signin response: ", response);
if (response.data.succ) {
return Alert.alert("로그인 성공");
} else {
// login fail ...
return Alert.alert("로그인 정보가 맞지 않습니다, 다시 입력해주세요");
}
} catch (e) {
const errorResponse = (e as AxiosError).response;
console.error(errorResponse);
// if (errorResponse) {
//   Alert.alert("알림", errorResponse.data.message);
// }
console.log("Hello world!");
return Alert.alert(`Error: ${JSON.stringify(e.response)}`);
}

这是后端代码的一部分

app.post("/userAuth/signin", async (req, res) => {
const { email, password } = req.body;
console.log(req.body);
isLocated = await userAuth.find({ email: email, password: password });
if (isLocated) {
res.status(200).json({ succ: true });
} else {
res.status(404).json({ succ: false });
}
});

这是控制台记录的输出,它是e.response

Hello world!
XMLHttpRequest {
"DONE": 4,
"HEADERS_RECEIVED": 2,
"LOADING": 3,
"OPENED": 1,
"UNSENT": 0,
"_aborted": false,
"_cachedResponse": undefined,
"_hasError": true,
"_headers": Object {
"accept": "application/json, text/plain, */*",
"content-type": "application/json",
},
"_incrementalEvents": false,
"_lowerCaseResponseHeaders": Object {},
"_method": "POST",
"_perfKey": "network_XMLHttpRequest_http://192.249.18.175:443/userAuth/signin",
"_performanceLogger": PerformanceLogger {
"_closed": false,
"_extras": Object {},
"_pointExtras": Object {},
"_points": Object {
"initializeCore_end": 1657466704543,
"initializeCore_start": 1657466704328,
},
"_timespans": Object {
"network_XMLHttpRequest_http://143.248.219.186:19000/logs": Object {
"endExtras": undefined,
"endTime": 1657466705712,
"startExtras": undefined,
"startTime": 1657466705643,
"totalTime": 69,
},
"network_XMLHttpRequest_http://143.248.219.186:19000/symbolicate": Object {
"endExtras": undefined,
"endTime": 1657466844264,
"startExtras": undefined,
"startTime": 1657466844224,
"totalTime": 40,
},
"network_XMLHttpRequest_http://192.249.18.175:443/userAuth/signin": Object {
"startExtras": undefined,
"startTime": 1657466713331,
},
},
},
"_requestId": null,
"_response": "Failed to connect to /192.249.18.175:443",
"_responseType": "",
"_sent": true,
"_subscriptions": Array [],
"_timedOut": false,
"_trackingName": "unknown",
"_url": "http://192.249.18.175:443/userAuth/signin",
"data": undefined,
"readyState": 4,
"responseHeaders": undefined,
"status": 0,
"timeout": 0,
"upload": XMLHttpRequestEventTarget {},
"withCredentials": true,
}
at node_modulesexpobuildenvironmentreact-native-logs.fx.js:27:4 in error
at node_modules@babelruntimehelpersregeneratorRuntime.js:86:13 in tryCatch
at node_modules@babelruntimehelpersregeneratorRuntime.js:66:31 in <anonymous>
at node_modules@babelruntimehelpersregeneratorRuntime.js:86:13 in tryCatch
at node_modules@babelruntimehelpersregeneratorRuntime.js:124:27 in invoke
at node_modules@babelruntimehelpersregeneratorRuntime.js:132:16 in PromiseImpl.resolve.then$argument_1
at node_modulesreact-nativenode_modulespromisesetimmediatecore.js:37:13 in tryCallOne
at node_modulesreact-nativenode_modulespromisesetimmediatecore.js:123:24 in setImmediate$argument_0
at node_modulesreact-nativeLibrariesCoreTimersJSTimers.js:248:12 in _allocateCallback$argument_0
at node_modulesreact-nativeLibrariesCoreTimersJSTimers.js:112:14 in _callTimer
at node_modulesreact-nativeLibrariesCoreTimersJSTimers.js:162:14 in _callReactNativeMicrotasksPass
at node_modulesreact-nativeLibrariesCoreTimersJSTimers.js:413:41 in callReactNativeMicrotasks
at node_modulesreact-nativeLibrariesBatchedBridgeMessageQueue.js:391:6 in __callReactNativeMicrotasks
at node_modulesreact-nativeLibrariesBatchedBridgeMessageQueue.js:133:6 in __guard$argument_0
at node_modulesreact-nativeLibrariesBatchedBridgeMessageQueue.js:368:10 in __guard
at node_modulesreact-nativeLibrariesBatchedBridgeMessageQueue.js:132:4 in flushedQueue

我成功连接到服务器和mongodb。

[nodemon] 2.0.19
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
Server started at 443
connected to mongoDB
你对这个问题有什么见解吗?
感谢您阅读这篇文章。

问题可能是您尝试通过HTTP发出请求。对于android,尝试像这样启用明文网络流量:

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:usesCleartextTraffic="true"
...>
...
</application>
</manifest>
类似地,在iOS上,你有NSAppTransportSecurity设置。您可以修改"信息"。列表文件如下:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

要小心,您应该只允许开发HTTP流量。请确保在生产环境中恢复这些更改。

相关内容

  • 没有找到相关文章

最新更新