title: CORS Node.js server
body: <p>I am totally new to Node.js and am just trying to get some example code running however I keep running into issues with CORS.</p>
  
  <p>Server.js</p>
  
  <pre><code>    var http = require('http');
      var io = require('socket.io');
  
   server = http.createServer(function(req, res){    
   });
  server.listen(8080);
  // socket.io
  var socket = io.listen(server,  { origins: '*:*' });
  
  // Enables CORS
  var enableCORS = function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
  res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-   Length, X-Requested-With');
  
  // intercept OPTIONS method
  if ('OPTIONS' == req.method) {
    res.send(200);
  }
  else {
    next();
  }
  };
  
  
  // enable CORS!
  socket.use(enableCORS);
  //--------------
  socket.on('connection', function(client){
    client.on('message', function(msg){
    socket.broadcast(msg);
    })
  }); 
  </code></pre>
  
  <p>client code:</p>
  
  <pre><code>var socket = io.connect('127.0.0.7:8080');
  socket.on('message', function(msg){
  alert(msg);
  console.log(msg);
  });
  </code></pre>
  
  <p>I tried installing the CORS module for node.js but I keep getting messages in firefox debugger, but I still keep getting this:</p>
  
  <pre><code>Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://8080/socket.io/1/?t=1400151675528. This can be fixed by moving the resource to the same domain or enabling CORS.
  ReferenceError: socket is not defined client2.html:8
  ReferenceError: socket is not defined
  </code></pre>