Missing Access-Control-Allow-Origin header on heroku

3.6k Views Asked by At

Some server code at start:

var util = require('util');
var express = require('express');
var http = require('http');
var sio = require('socket.io');

var app = express();
var server = http.createServer(app);
var io = sio.listen(server);

console.log('origins-> '+io.origins());
//io.set("origins","*:*");
io.origins("*:*");

server.listen(process.env.PORT || 80, function (){
    var addr = server.address();
    console.log('App listening '+JSON.stringify(addr));
});

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
console.log("user connected");

  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
  });
});

Here is git repo: https://github.com/Foxbond/heroku-socket-test

{heroku} means http://foxclick.heroku.com

Error form chrome console:

XMLHttpRequest cannot load {heroku}/socket.io/?EIO=3&transport=polling&t=1416327810844-0. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '{heroku}' is therefore not allowed access.

Response headers:

HTTP/1.1 301 Moved Permanently
Connection: keep-alive
Server: Cowboy
Date: Tue, 18 Nov 2014 16:23:34 GMT
Content-Length: 0
Location: {heroku}/socket.io/?EIO=3&transport=polling&t=1416327810844-0

Everything works on localhost with these response headers:

HTTP/1.1 200 OK
Content-Type: application/octet-stream
Content-Length: 101
Access-Control-Allow-Origin: *
Set-Cookie: io=cFBQp1g1WCDbE0_1AAAA
Date: Tue, 18 Nov 2014 16:28:03 GMT
Connection: keep-alive

I have no idea should I do, I think I already tried every possible solution. Thanks for any help!

1

There are 1 best solutions below

2
On

i had a similar problem try putting the html on a webserver like xampp or Apache.

Usually with sockets when i run them if I run the html locally with the socket server I get the same No 'Access-Control-Allow-Origin'. But if you use a webserver this should not happen