node.js - Access-Control-Allow-Origin error on two nodejs Servers -


I connected the page to the socket IO connection, the witch created by the node JS server, listening to the socket, to other node servers Listening to IO

There are 2 works in this idea: Generating HTML templates by an Express Server Another Express Server supports Socket IO

Here is the server .as code:

  const app = is required ('Express'); Const util = requirement ('use'); Const http = requires ('http'); Const fs = requirement ('fs'); Const debug = console.log; Const moment = requirement ('moment'); Const incident = requirement ('events'). EventEmitter; Var server = app (); Server.use (function (req, res, next) {res.setHeader ('Access-Control-Allow-Origin', '*'); res.setHeader ('Access-Control-Allow-Methods',' GET, POST Res -setHeader ('access-control-add-headers', 'X-requested-content-type'); res.setHeader ('access-control-acceptable-credentials', true); next ();}) ; Server.use (app.json ()); server.use (app.urlencoded ()); server.use (app.logger ()); server.use (app.favicon ()); Server.use (app) .stitic (__derename + '/ core')); Server.get ('*', function (req, res, next) {debug ('Connection refused: \ t' + req.url); var r = (e .url.split ( '/') [2] = = 'Socket.io.js')? Server.st.socket: server.st.tmp; // return to TMP-HTML file or socket Return js file resend (r); res.end ();}); server.st = {socket: fs.readFileSync ('./ socket.io.js', 'utf-8') tmp: fs. readFileSync ( './tmp.html', 'utf-8')} server listening (81); *************************** *********************** when = ( 'socket.io') require ************** ****************************************************************************************************** io.configure (function () {io.set ( 'log level', 1); io.set ( 'Basic', '*');}); Io.sockets.on ('connection', function (socket) {debug ('IT is work')})  

Here is the tmp.html code:

  & lt ;! DOCTYPE html & gt; & Lt; Html & gt; & Lt; Top & gt; & Lt; Script src = "/ socket.io/socket.io.js"></script> & Lt; Script & gt; Var socket = io.connect ('http: // localhost: 82'); & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; / Body & gt;  

and default Socket.io.js customer

and the client side error

 Can not load  XMLHttpRequest http: // local host: 82 / socket.io / EIO = 2 & amp; Transport = voting There is no 'access-control-permission-generation' header on the requested resource Origin 'http: // localhost: 81' therefore the entry is not allowed  

What's wrong with this * ?

The answer was:

HTTP server must be socket support, the default The original settings have been changed to socket option, which can not be changed by var IO configurate, manager.js in the 778 line (v0.9), you have to type in the original option: header ['access-control- Permission-origin '] =' * '; Or

The best way to resolve this is:

The HTTP server that should support the socket has tied the address with its own listener, which is a socket bitwen client The socket.io.js file that creates this tunnel, that I meet with the local host: "Why do I get a socket client file from the server, irrelevanted to that server socket?": 81. This is the reason I have asked for my own and changed

   

And it works THX, goodbye with great love from Russia.


Comments