0

I create a basic nodejs website on azure last days. I would like to have a server dedicate for nodejs, and on the other hand I have a classic HTML5 page on another domain.

The matter is that I have a CORS (cross origin problem) when my page try to connect to the azure website. Any solutions ? This is a bit of my code :

Server :

var express = require('express');
var path = require('path');
var app = express();

var server = require('http').createServer(app).listen(process.env.PORT || 5000);
var io = require('socket.io').listen(server);

Client :

var socket = io.connect('https://****.azurewebsites.net/');

Thanks for your time,

Regards.

Sw1a
  • 245
  • 4
  • 15
  • Did you do the appropriate things to enable CORS for your webSocket server so the browser will permit the cross origin connection to your webSocket server? – jfriend00 Jan 15 '15 at 09:21
  • A simple Google search for CORS and Express or CORS and socket.io will show you how to do it. Here's are a couple links from that search: https://github.com/troygoode/node-cors/ and http://stackoverflow.com/questions/24058157/socket-io-node-js-cross-origin-request-blocked – jfriend00 Jan 15 '15 at 21:37

1 Answers1

1

From my experience the easiest way to enable CORS is with cors npm package.

Include cors in your package.json or run $ npm install --save cors.

Then include the following in your server code.

var cors = require('cors');
app.use(cors());
Razzildinho
  • 2,564
  • 1
  • 19
  • 32