title: CORS Blocked with node.js and socket.io
body: <p>I recently started learning node.js and socket.io. I followed a simple tutorial that socket.io had and it all worked fine while running on my computer. However, I decided to upload the client part to a server for testing and that is where the problems began. I would like to run the chat client on a web host, and run the server on my computer, or another host. Basically, I plan on port forwarding the server, and having the client run on a web page. I opened my port to port forward and it seems to be working, however I am getting the error on the web page every time.</p>
  
  <pre><code>Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://24.151.51.34:3000/socket.io/?EIO=3&amp;transport=polling&amp;t=1437399007343-0. (Reason: CORS request failed).
  </code></pre>
  
  <p>I've been messing around with the code in hopes of finding a solution to this problem before starting my own project, however I can't figure out a way. The client code is :</p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;html&gt;
    &lt;head&gt;
      &lt;title&gt;Socket.IO chat&lt;/title&gt;
      &lt;style&gt;
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font: 13px Helvetica, Arial; }
        form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
        form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
        form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
        #messages { list-style-type: none; margin: 0; padding: 0; }
        #messages li { padding: 5px 10px; }
        #messages li:nth-child(odd) { background: #eee; }
      &lt;/style&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;ul id="messages"&gt;&lt;/ul&gt;
      &lt;form action=""&gt;
        &lt;input id="m" autocomplete="off" /&gt;&lt;button&gt;Send&lt;/button&gt;
      &lt;/form&gt;
      &lt;script src="https://cdn.socket.io/socket.io-1.2.0.js"&gt;&lt;/script&gt;
      &lt;script src="http://code.jquery.com/jquery-1.11.1.js"&gt;&lt;/script&gt;
      &lt;script&gt;
        var socket = io('24.151.51.34:3000');
        $('form').submit(function(){
          socket.emit('chat message', $('#m').val());
          $('#m').val('');
          return false;
        });
        socket.on('chat message', function(msg){
          $('#messages').append($('&lt;li&gt;').text(msg));
        });
      &lt;/script&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>For the server code I tried to add code to allow CORS but really not sure what to do :</p>
  
  <pre><code>var app = require('express')();
  var http = require('http').Server(app);
  var io = require('socket.io')(http);
  
  io.set('origins', 'http://browsercombat.com:80');
  
  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");
      res.header("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS");
      res.header("Access-Control-Allow-Credentials", "true");
      next();
  });
  
  app.get('/', function(req, res, next) {
      res.sendFile(__dirname + '/index.html');
  });
  
  app.post('/', function(req, res, next) {
      // Handle the post for this route
  });
  
  io.on('connection', function(socket){
    socket.on('chat message', function(msg){
      io.emit('chat message', msg);
    });
  });
  
  http.listen(3000, function(){
    console.log('listening on *:3000');
  });
  </code></pre>