我在 react 中创建了一个前端,在 express nodejs 中创建了一个后端,并且我正在使用 socket.io 来让客户端和服务器进行通信。
当我加载react页面时,我每秒都会收到以下错误:
Failed to load resource: the server responded with a status of 404 (Not Found)
我已将前端设置为在端口 3000 上运行,将服务器设置为在端口 3500 上运行。失败的连接请求正在发送到以下地址: http://localhost:3000/socket.io/?EIO=3&transport=polling&t=MoHUt8D
以下是节点后端的设置:
const bodyParser = require('body-parser');
const https = require('https');
const fs = require('fs');
const express = require('express');
const app = express();
app.use(bodyParser.json());
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin',
'https://www.localhost:3000');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.header('Access-Control-Allow-Methods', 'GET, POST, DELETE, PATCH, PUT');
res.header('Access-Control-Allow-Credentials', 'true');
next();
});
const server = https.createServer(app);
var io = require('socket.io')(server);
console.log('Server listening on port 3500.');
server.listen(3500);
客户端连接如下:
import openSocket from "socket.io-client";
const hostURL = "http://localhost:3500";
const socket = openSocket()
关于如何停止错误并使前端和后端与 socket.io 一起工作的任何建议