0

My node.js server works beautifully on my live website "https://www.example.com", however the development environment throws the following console error:

"XMLHttpRequest cannot load https://dev.example.com:2083/socket.io/?EIO=3&transport=polling&t=LtTLz1_. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://dev.example.com' is therefore not allowed access. The response had HTTP status code 522."

I'm using cloudflare and I have two node.js servers running, the live server runs on port 2053 and the dev runs on port 2083.

I tried several methods to add the header Access-Control-Allow-Origin:* and nothing seems to work.

Here is the top portion of my node.js script:

var fs = require('fs');

var options = {
    ca: fs.readFileSync(__dirname + '/ca.pem'),
  key: fs.readFileSync(__dirname + '/file.pem'),
  cert: fs.readFileSync(__dirname + '/file.crt')
};

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

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

var https = require('https'),
server = https.createServer(options, app),
io = require('socket.io').listen(server);

server.listen(2083);

Is there something I'm missing here?

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Kevin J
  • 194
  • 1
  • 11
  • 2
    It's a cloudflare connection timeout problem, not a CORS problem (double check the configuration) – samsonthehero Aug 13 '17 at 20:54
  • If you are trying to make a socket.io connection to a host that is different than the web page was loaded from, then you need to either add CORS support to your socket.io server (to allow the cross origin connection) or you need make socket.io connect only with a webSocket (no initial polling) because webSocket connections are not subject to CORS. – jfriend00 Aug 13 '17 at 22:21
  • Here's how to force socket.io to only use a webSocket and avoid CORS: [Socket.io 1.x: use WebSockets only?](https://stackoverflow.com/questions/28238628/socket-io-1-x-use-websockets-only/28240802#28240802) and [How to configure socket.io for CORS](https://stackoverflow.com/a/38749535/816620). – jfriend00 Aug 13 '17 at 22:23

1 Answers1

0

Turns out it has nothing to do with cloudflare. I'm on a WHM/cpanel server and ports 2083 (SSL) and 2087 (SSL) are used by WHM.

I used port 8443 instead and it works beautifully.

So in conclusion, while cloudflare has 2083 and 2087 open, don't use these in conjunction with cpanel.

Kevin J
  • 194
  • 1
  • 11