Socket.io No header "Access-Control-Allow-Origin" is present on the requested resource. The origin of "http: // localhost" is therefore not allowed

I am trying to learn nodejs with socket.io, and for now I am using this tutorial from GianlucaGuarini . When I enter my client.html file, the following error appears. I know what this means and that it exists to prevent Cross Browser scripts, but I don’t know how to allow my nodejs script to access the client.html file.

XMLHttpRequest cannot load http://localhost:8000/socket.io/?EIO=3&transport=polling&t=1422653081432-10. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. 

Here is part of my socket code.

  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: '', database: 'database', port: 3306 }), POLLING_INTERVAL = 3000, pollingTimer; // If there is an error connecting to the database connection.connect(function(err) { // connected! (unless `err` is set) 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) { res.writeHead(200, { /// ... 'Access-Control-Allow-Origin' : '*' }); 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); }); } 

Does anyone know how I can solve my problem?

Regards / H

+5
source share
3 answers

First of all, stop using writeHead everywhere. Because it completely rewrites the response headers.

If the tour is written as follows:

 res.writeHead(200,{"coolHeader":"YesIAm"}); res.writeHead(500); 

then node.js will only send a response with a status of 500 and without the "coolHeader" header;

If you want to change the status code, use

 res.statusCode = ###; 

If you want to add a new header use

 res.setHeader("key", "value"); 

And if you want to rewrite all the headers, use writeHeader(...)

Secondly. Add this code

 res.statusCode = 200; //... res.setHeader("Access-Control-Allow-Origin", "*"); res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); 

instead

  res.writeHead(200, { /// ... 'Access-Control-Allow-Origin' : '*' }); 

and replace all writeHead(###) with res.statusCode = ###;

+5
source

Try customizing the `Access-Control-Allow-Origin 'header of your response object in Node.

 response.writeHead(200, { /// ... 'Access-Control-Allow-Origin' : '*' }); 
+4
source

It looks like you are calling .listen for both the application and socket.io (I believe this is redundant as you are expanding your server with socket.io)

I have a small piece that works fine for me using socket.io 1.x I like to use https as it kills some problems with firewalls and antiviruses, but this example has been rewritten to http.

 var http = require('http'), socketio = require('socket.io'), options={}, port=8080; //start http var app = http.createServer(options, handler), io = socketio(app, { log: false, agent: false, origins: '*:*' // 'transports': ['websocket', 'htmlfile', 'xhr-polling', 'jsonp-polling'] }); app.listen(port); console.log('listening on port ' + port); 
+2
source

Source: https://habr.com/ru/post/1212305/


All Articles