1

I am a newbie with node.js and I am trying to set up my first installation. I have a server.js where I define a new server:

var app = require('http').createServer(handler),
  io = require('socket.io').listen(app),
  fs = require('fs'),
  mysql = require('mysql'),
  connectionsArray = [],
  connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'flipper',
    database: 'oclock',
    //database: 'nodejs',
    port: 3306
  }),
  POLLING_INTERVAL = 3000,
  pollingTimer;

// If there is an error connecting to the database
connection.connect(function(err) {
  // connected! (unless `err` is set)
  if (err) {
    console.log(err);
  }
});

// creating the server ( localhost:8000 )
app.listen(8000);

// on server started we can load our client.html page
function handler(req, res) {
  fs.readFile(__dirname + '/client.html', function(err, data) {
    if (err) {
      console.log(err);
      res.writeHead(500);
      return res.end('Error loading client.html');
    }
    res.writeHead(200);
    res.end(data);
  });
}

Next I create a client.html file that should connect to the server and retrieve the data:

<div id="container">Loading ...</div>
<script src="node_modules/socket.io/node_modules/socket.io-client/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
// create a new websocket
var socket = io.connect('http://oclock.dyndns.org:8000');
// on message received we print all the data inside the #container div
socket.on('notification', function (data) {
    var usersList = "<dl>";
    $.each(data.users,function(index,user){
        usersList += "<dt>" + user.user_name + "</dt>\n" +
                     "<dd>" + user.user_description + "\n" +
                        "<figure> <img class='img-polaroid' width='50px' src='" + user.user_img + "' /></figure>"
                     "</dd>";
    });
    usersList += "</dl>";
    $('#container').html(usersList);

    $('time').html('Last Update:' + data.time);
  });
</script>

When I try to connect to the server using client.html using the url http://oclock.dyndns.org/client.html I see that the request is blocked for a cross origin request. The request url is http://oclock.dyndns.org:8000/socket.io/?EIO=3&transport=polling&t=1428917662987-0.

What am I missing? What am I doing wrong?

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74

1 Answers1

1

Origins are based on:

  • Scheme (http in both cases)
  • Hostname (oclock.dyndns.org in both cases)
  • Port Number (80 on one, 8000 on the other)

So you do have different origins. Set up CORS as usual.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I have tried to set up cors adding the following Header set Access-Control-Allow-Origin "*" to the virtual host definition for this specific server in Apache2 but with no luck. Should I do something on node.js too? – Lelio Faieta Apr 13 '15 at 09:50
  • Yes. You are making the request to your Node server. Your Node server has to give your Apache server permission to access it, not the other way around. – Quentin Apr 13 '15 at 09:51