node.js - Node.js 中 Node 静态的请求资源上不存在 'Access-Control-Allow-Origin' header

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


});

我的服务器在 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. 

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

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

最佳答案

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

解释 1:原点设置不正确

改变

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

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

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

解释2:标题输出不正确

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

确保 header 正确。 Here's a list您需要设置的内容(对于 PHP,但 header 相同)。

关于node.js - Node.js 中 Node 静态的请求资源上不存在 'Access-Control-Allow-Origin' header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23991562/

相关文章:

javascript regexp 负向后查找在某些情况下不匹配

jquery - 将 XMLHttpRequest 错误保存到 JS 变量

node.js - 使用restify时如何支持cors

php - API 安全 - 使用 api key secret 、散列、tnonce 等 VS。 SSL证书验证

node.js - 每次运行 cluster.fork() 时,我都会收到错误 : bind EADDRINUSE

node.js - Jade 中的 Flash 消息

来自原点的 Angular 和 WebApi Cors 请求已被 CORS 策略阻止

jquery - 使用 JSONP 或 CORS 的跨域 JavaScript 调用

javascript - 在 Angular Chrome 扩展的 DOM 中加载选项卡图标成功,但显式 XHR 请求失败

node.js - 通过多个页面将变量保存在socket.io中