协同编辑文档-ReactJS

时间:2018-11-17 16:50:56

标签: node.js reactjs express

我正在使用React JS编写一份相关的文档,并且正在遵循本教程:

https://medium.com/@ethanryan/making-a-simple-real-time-collaboration-app-with-react-node-express-and-yjs-a261597fdd44

https://github.com/ethanryan/textedit-app

它使用YJS进行并发编辑。当我在端口3000上运行服务器,而我的客户端在端口3001上运行时,它在客户端检查中带有错误:

localhost/:1 Access to XMLHttpRequest at 'http://localhost:3000/socket.io/? 
EIO=3&transport=polling&t=1542472628946-27' from origin 
'http://localhost:3001' has been blocked by CORS policy: 

The value of the 'Access-Control-Allow-Origin' header in the response must 
not be the wildcard '*' when the request's credentials mode is 'include'. 
The credentials mode of requests initiated by the XMLHttpRequest is 
controlled by the withCredentials attribute.

当我扩展错误时,我还在服务器端检查此错误,并获得了很多信息:

GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1542472716430-16004 net::ERR_NETWORK_CHANGED

GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1542472716442-16005 net::ERR_NETWORK_CHANGED

POST http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1542472716697-16006 404 (Not Found)

POST http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1542472716709-16007 404 (Not Found)

我搜索了此错误,发现我需要执行以下操作:

下载chrome扩展程序

Chrome Extension Allow-Control-Allow-Origin: *

或使用:

var allowCrossDomain = function(req, res, next) {
 res.header('Access-Control-Allow-Origin', "*");
 res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
 res.header('Access-Control-Allow-Headers', 'Content-Type');
 next();
}

我已经下载了cors扩展名,但仍然出现上述错误。但是,由于我使用的是YJS,因此服务器就在他们的身边,即我只是在 websocket服务器文件夹中的终端上启动npm >。

我不确定如何解决此问题。

0 个答案:

没有答案