Node.js中node-static中请求的资源上没有"Access-Control-Allow-Origin"标头

use*_*005 5 node.js cors

我对node.js很新,我不知道如何克服这个问题.希望您能够帮助我.

我有一个在node.js中运行的服务器,它有node-static和socket.io.代码如下.

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)');
});


});
Run Code Online (Sandbox Code Playgroud)

我的服务器在localhost:2013中运行.我的网页是在localhost:8080.当我尝试访问服务器时,出现以下错误

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. 
Run Code Online (Sandbox Code Playgroud)

我知道这是由于CORS.但我添加了代码行来适当地设置标题.当我检查这个错误时,大多数人都说如何用node.js中的express来改进它,而不是node-static.

当我从不同的域访问时,有人可以向我建议我接下来可以从服务器获得响应.

Per*_*ian 0

对于这个问题可能有几种可能的解释:

解释1:原点设置不正确

改变

res.setHeader('Access-Control-Allow-Origin', 'http://172.26.191.45:8080');
Run Code Online (Sandbox Code Playgroud)

res.setHeader('Access-Control-Allow-Origin', 'localhost');
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,请在服务器端输出请求源并将“localhost”替换为该源。(我不知道足够的节点来告诉你如何输出原点)。

解释2:标头输出不正确

仔细检查从服务器返回的标头。您可以通过安装一些允许您查看请求标头的浏览器插件来做到这一点。

确保标题正确。以下是您需要设置的列表(对于 PHP,但标头是相同的)。