6

I'm running node.js with socket.io on apache. I have it running on a subdomain (e.g - myapp.mydomain.com:8000) as you can see I'm running it on a different port so that I can access it using myapp.mydomain.com.

Everything works fine in chrome, Opera, IE but FF fails and I get a cross domain error.

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://myapp.mydomain.com:8000/socket.io/?EIO=2&transport=polling&t=1429260923523-0. This can be fixed by moving the resource to the same domain or enabling CORS.

Any ideas?

UPDATE:

Added my code as requested:

function handler(req, res) {
  fs.readFile('../index.html', function(err, data) {
    if (err) {
      console.log(err);
      res.writeHead(500);
      return res.end('Error loading client.html');
    }
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    res.writeHead(200);
    res.end(data);
  });
}
user3849687
  • 107
  • 1
  • 7
  • Did you set any CORS headers? If so, could you add them to your question? – aaronk6 Apr 17 '15 at 09:44
  • You cannot have `Access-Control-Allow-Headers` twice. Should be `res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");`. – aaronk6 Apr 17 '15 at 11:15
  • 1
    possible duplicate of [Socket.io + Node.js Cross-Origin Request Blocked](http://stackoverflow.com/questions/24058157/socket-io-node-js-cross-origin-request-blocked) – aaronk6 Apr 17 '15 at 11:18
  • checked that and did as recommended. Still no joy! – user3849687 Apr 17 '15 at 13:06

1 Answers1

0

Websockets (Socket.IO) is not depend upon CORS. The one of main reason behind Socket.IO is to completely stop CORS kind of issues while dealing with different domains.If you think this can be done by enabling cors, then try this assuming you are using express.js or hapi.js, see this links below to how to enable cors.

hapijs -->

new Hapi.Server({
  connections: {
    routes: {
      cors: true
    }
  }
})

expressjs -->

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});
Sathish
  • 2,056
  • 3
  • 26
  • 40