Heim > Fragen und Antworten > Hauptteil
Ich habe eine App, die mit React, Node.js und Socket.io erstellt wurde
Ich habe das Node-Backend für Heroku und das Frontend für Netlify bereitgestellt
Ich weiß, dass die CORS-Fehler mit dem Server zusammenhängen, aber egal, was ich hinzufüge, der Fehler im Bild unten wird dadurch nicht behoben.
Ich habe das Proxy-Skript auch als „proxy“ in Reacts package.json hinzugefügt: „https://googledocs-clone-sbayrak.herokuapp.com/“
Das ist meine server.js
Datei;
const mongoose = require('mongoose'); const Document = require('./Document'); const dotenv = require('dotenv'); const path = require('path'); const express = require('express'); const http = require('http'); const socketio = require('socket.io'); dotenv.config(); const app = express(); app.use(cors()); const server = http.createServer(app); const io = socketio(server, { cors: { origin: 'https://googledocs-clone-sbayrak.netlify.app/', methods: ['GET', 'POST'], }, }); app.get('/', (req, res) => { res.status(200).send('hello!!'); }); const connectDB = async () => { try { const connect = await mongoose.connect(process.env.MONGODB_URI, { useUnifiedTopology: true, useNewUrlParser: true, }); console.log('MongoDB Connected...'); } catch (error) { console.error(`Error : ${error.message}`); process.exit(1); } }; connectDB(); let defaultValue = ''; const findOrCreateDocument = async (id) => { if (id === null) return; const document = await Document.findById({ _id: id }); if (document) return document; const result = await Document.create({ _id: id, data: defaultValue }); return result; }; io.on('connection', (socket) => { socket.on('get-document', async (documentId) => { const document = await findOrCreateDocument(documentId); socket.join(documentId); socket.emit('load-document', document.data); socket.on('send-changes', (delta) => { socket.broadcast.to(documentId).emit('receive-changes', delta); }); socket.on('save-document', async (data) => { await Document.findByIdAndUpdate(documentId, { data }); }); }); console.log('connected'); }); server.lis ten(process.env.PORT || 5000, () => console.log(`Server has started.`) );
Hier stelle ich Anfragen an das Frontend;
import Quill from 'quill'; import 'quill/dist/quill.snow.css'; import { useParams } from 'react-router-dom'; import { io } from 'socket.io-client'; const SAVE_INTERVAL_MS = 2000; const TextEditor = () => { const [socket, setSocket] = useState(); const [quill, setQuill] = useState(); const { id: documentId } = useParams(); useEffect(() => { const s = io('https://googledocs-clone-sbayrak.herokuapp.com/'); setSocket(s); return () => { s.disconnect(); }; }, []); /* below other functions */ /* below other functions */ /* below other functions */ }
P粉0388567252023-12-15 15:50:11
看来您还没有导入 cors 包。是从其他地方进口的吗?
var cors = require('cors') // is missing
P粉6748763852023-12-15 10:51:51
https://googledocs-clone-sbayrak.netlify.app/
不是起源。删除结尾的斜杠。
Origin
标头的值中不允许有尾部斜杠根据CORS协议(在Fetch标准中指定),浏览器永远不会将 Origin
请求标头设置为带有尾部斜杠的值。因此,如果 https://googledocs-clone-sbayrak.netlify.app/whatever
上的页面发出跨源请求,则该请求的 Origin
标头将包含 p>
https://googledocs-clone-sbayrak.netlify.app
没有任何尾部斜杠。
您正在使用Socket.IO,依赖于 节点.js cors
包。如果请求的来源与您的 CORS 配置的 origin
值 (https ://googledocs-clone-sbayrak.netlify.app/
)。
显然,
'https://googledocs-clone-sbayrak.netlify.app' ===
'https://googledocs-clone-sbayrak.netlify.app/'
评估为false
,这会导致cors
包不在响应中设置任何Access-Control-Allow-Origin
标头,这导致浏览器中的 CORS 检查失败,因此出现您观察到的 CORS 错误。
Fetch 标准的第 3.2.5 节甚至提供了 这个错误的一个有启发性的例子,
Access-Control-Allow-Origin: https://rabbit.invalid/
并解释了为什么它会导致 CORS 检查失败: