node.js - Why does socket.io client give XHR polling error: blocked by cors policy when using self-hosted server address? - Stac

I'm trying to self-host a node.js socket.io (v 4.8.1) application (not using Express). Everything

I'm trying to self-host a node.js socket.io (v 4.8.1) application (not using Express). Everything works fine locally, when the server and client are on localhost:8000 and localhost:5173 respectively, but when I try to make these public (using zrok) and replacing the links between the two, I get

Access to XMLHttpRequest at 'https://<server share name>.share.zrok.io/socket.io/?EIO=4&transport=polling&t=pxmzu36h' from origin 'https://<client share name>.share.zrok.io' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Using the following on the client side gives me an XHR polling error

socket.on("connect_error", (err:Error) => {
  console.log(err.message);
  console.log(err.context);
});

I've tried making sure that access-control-allow-origin is set. On the server, I am using

const httpServer = createServer(function (req, res) {
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
  res.setHeader("Access-Control-Allow-Headers", "Content-Type");
  res.setHeader('Access-Control-Max-Age', 2592000); // 30 days
  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify({ message: 'CORS enabled' }));
}); // Create HTTP server

const io = new Server(httpServer, {
  cors: {
    origin: "*",
    methods: ["GET", "POST"]
  }
});

On the front (vuejs + vite), I have allowedHosts set in vite.config.ts and switched out the localhost address of the server to the https://<client share name>.share.zrok.io one.

I'm trying to self-host a node.js socket.io (v 4.8.1) application (not using Express). Everything works fine locally, when the server and client are on localhost:8000 and localhost:5173 respectively, but when I try to make these public (using zrok) and replacing the links between the two, I get

Access to XMLHttpRequest at 'https://<server share name>.share.zrok.io/socket.io/?EIO=4&transport=polling&t=pxmzu36h' from origin 'https://<client share name>.share.zrok.io' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Using the following on the client side gives me an XHR polling error

socket.on("connect_error", (err:Error) => {
  console.log(err.message);
  console.log(err.context);
});

I've tried making sure that access-control-allow-origin is set. On the server, I am using

const httpServer = createServer(function (req, res) {
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
  res.setHeader("Access-Control-Allow-Headers", "Content-Type");
  res.setHeader('Access-Control-Max-Age', 2592000); // 30 days
  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify({ message: 'CORS enabled' }));
}); // Create HTTP server

const io = new Server(httpServer, {
  cors: {
    origin: "*",
    methods: ["GET", "POST"]
  }
});

On the front (vuejs + vite), I have allowedHosts set in vite.config.ts and switched out the localhost address of the server to the https://<client share name>.share.zrok.io one.

Share Improve this question edited Mar 25 at 1:20 Phil 165k25 gold badges262 silver badges267 bronze badges asked Mar 25 at 0:09 user124173user124173 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Just figured it out! This was actually more of an issue with zrok, which I was using to host my server- there was a warning page before accessing the server which I didn't bypass yet (open share- adding the header skip_srok_interstitial)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744223432a4563892.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信