2

so i have a server set up using hapi js and socket.io

var hapi = require('hapi');
var server = new hapi.Server();
server.connection({
    host : 'localhost',
    port: 3000,
    routes: {cors: true}
});
var io = require("socket.io")(server.listener);

on the client

var io = socket.connect(':3000');

when i access from localhost everything works fine, but when i access from the external IP i get the CORS error and can't make it work, on the server i've tried io.set( "origins", "*:*" ); didn't work, and io.set('transports', ['websocket', 'xhr-polling', 'jsonp-polling', 'htmlfile', 'flashsocket']); also didn't work and throws a bad request error on the browser. am i missing something? thanks you very much for your time.

2 Answers2

6

Can you try setting the origin explicitly instead of using "*"? You probably don't want to use "*" in production anyway.

Try using:

server.connection({
  host : 'localhost',
  port: 3000,
  routes: {
    cors: {
      origin: ['your_origin_here']
    }
  }
});

Also sometimes there is an OPTIONS preflighted request made to check the origin, maybe share that as well to make sure CORS is being set properly?

Kevin Wu
  • 8,461
  • 5
  • 28
  • 28
0

so the problem was setting the host to localhost, instead i used 0.0.0.0 as explained here

Community
  • 1
  • 1