【问题标题】:React-Native not able to connect to a Node.js server (Network request failed onerror)React-Native 无法连接到 Node.js 服务器(网络请求失败 onerror)
【发布时间】:2018-12-11 05:10:23
【问题描述】:

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

但我收到“网络请求失败”错误...

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

感谢您的帮助或建议!

这是我的应用代码:

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);
});

【问题讨论】:

    标签: node.js react-native socket.io


    【解决方案1】:

    试试你的 IP 地址而不是 localhost

    例如:

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

    【讨论】:

    • 我也试过了,可惜没用...不过感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 2021-04-07
    • 2020-07-23
    • 2020-04-27
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多