Tuesday 27 June 2017

Getting CORS error while using socket.io / nginx / node

I'm getting this error log from chrome's consoleXMLHttpRequest cannot load http://ift.tt/2sO2vv4. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 500. I am using Node.js, socket.io to talk between my node and react.js, with a digitalocean's droplet using nginx to host them.I've been reading a lot about CORS errors and I am unsure where to fix the error. I've been trying to allow them in my NGINXlocation /server { proxy_pass http://localhost:8080; proxy_http_version 1.1; proxy_set_header Access-Control-Allow-Origin *; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } And from my node.js, server side:var express = require("express"); var app = express(); var http = require("http"); var port = 8080; var io = require("socket.io").listen(app.listen(port)); app.use("/", function (req, res, next) { res.status(200).send("Online |" + " Version : [" + AppVersion + "]"); res.setHeader("Access-Control-Allow-Origin","*"); res.setHeader("Access-Control-Allow-Headers","X-Requested-With,content-type"); res.setHeader("Access-Control-Allow-Methods","GET,POST, OPTIONS, PUT, PATCH, DELETE"); next();}); And I connect on the client side using:const socket = io.connect("http://ift.tt/2thikLM") I am not really sure where and what I should look for. Any kind of help would help. Thanks! First time posting in /r/node, sorry if this don't belong here.

Submitted June 27, 2017 at 08:02PM by KerryGD

No comments:

Post a Comment