React Native无法连接到Node.js服务器(网络请求失败或错误)



我正在尝试制作一个简单的React Native应用程序,它可以将数据发送到Node.js服务器。

尽管如此,我还是收到了一个"网络请求失败"错误。。。

这是我第一次尝试实施这种项目,所以我可能犯了一个愚蠢的错误,但我已经两天找不到了

感谢您的帮助或建议!

这是我的应用程序代码:

import React from 'react'
import {Text, View, TextInput, Button, StyleSheet, ImageBackground, Image, TouchableOpacity} from 'react-native' //import des components natifs
import {StackNavigator } from 'react-navigation';
const io = require('socket.io-client');
let socket = io('https://localhost:8080');
class Connexion extends React.Component{
constructor(props){
super(props);
}
validationConnexion(){
fetch('https://localhost:8080', {
method: 'POST',
headers: {
Accept: 'text/plain',
},
}).catch((error) => {             
console.error(error);
});
}

在后端:

const express = require('express');
const https = require('https')
const socketio = require('socket.io');
const app = express();
const server = https.Server(app, function(req, res) {
res.writeHead(200, {"Content-Type": "text/plain"});
res.end();})
const websocket = socketio(server);
server.listen(8080, () => console.log('listening on *:8080'));
// The event will be called when a client is connected.
websocket.on('connection', (socket) => {
console.log('A client just joined on', socket.id);
});

尝试您的IP地址而不是localhost

例如:

https://localhost:8080到https://192..*:8080

最新更新