简体   繁体   中英

No 'Access-Control-Allow-Origin' header is present on the requested resource in node-static in Node.js

I am quite new to node.js and I don't know how to overcome this issues. Hope you can help me.

I have a server runing in node.js which has node-static and socket.io. The code is as follows.

var numClients=0;
var static = require('node-static');
//var static = require('express');
var http = require('http');
var file = new(static.Server)();
var app = http.createServer(function (req, res) {
res.setHeader('Access-Control-Allow-Origin', 'http://172.26.191.45:8080');

// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
file.serve(req, res);
}).listen(2013);


var io = require('socket.io').listen(app);


io.sockets.on('connection', function (socket){

function log(){
    var array = [">>> "];
    for (var i = 0; i < arguments.length; i++) {
        array.push(arguments[i]);
    }
    socket.emit('log', array);
}

socket.on('message', function (message) {
    log('Got message: ', message);
    socket.broadcast.emit('message', message); // should be room only
});


socket.on('create or join', function (room) {
    numClients=numClients+1;

    log('Room ' + room + ' has ' + numClients + ' client(s)');
    log('Request to create or join room', room);

    if (numClients == 0){
        socket.join(room);
        socket.emit('created', room);
    } else if (numClients == 1) {
        io.sockets.in(room).emit('join', room);
        socket.join(room);
        socket.emit('joined', room);
    } else { // max two clients
        socket.emit('full', room);
    }
    socket.emit('emit(): client ' + socket.id + ' joined room ' + room);
    socket.broadcast.emit('broadcast(): client ' + socket.id + ' joined room ' + room);

});

socket.on('disconnect', function () {
    numClients=numClients-1;
    log('Room has ' + numClients + ' client(s)');
});


});

My server is running in localhost:2013. My webpage is in localhost:8080. When I try to access the server, I get the following error

XMLHttpRequest cannot load https://computeengineondemand.appspot.com/turn?username=41784574&key=4080218913. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://172.26.191.45:8080' is therefore not allowed access. 

I know this is due to CORS. but I have added the code lines to set the Headers appropriately. When I check on this error most people say how to improve it with express in node.js but not node-static.

Can someone suggest to me what I can do next to get a response from the server when I'm accessing from a different domain.

There could be several possible explanations for this issue:

Explanation 1: Origin not set correctly

Change

res.setHeader('Access-Control-Allow-Origin', 'http://172.26.191.45:8080');

To

res.setHeader('Access-Control-Allow-Origin', 'localhost');

If that doesn't work, output the request origin on the server side and replace 'localhost' with this origin. (I don't know enough node to tell you how to output the origin).

Explanation 2: Headers not outputted correctly

Double check the headers returned from the server. You can do this by installing some browser plugin which allows you to view request headers.

Make sure the headers are correct. Here's a list of what you need to set (for PHP, but the headers are the same).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM