简体   繁体   English

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

[英]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. 我是node.js的新手,我不知道如何克服这个问题。 Hope you can help me. 希望您能够帮助我。

I have a server runing in node.js which has node-static and socket.io. 我有一个在node.js中运行的服务器,它有node-static和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. 我的服务器在localhost:2013中运行。 My webpage is in localhost:8080. 我的网页是在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. 我知道这是由于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. 当我检查这个错误时,大多数人都说如何用node.js中的express来改进它,而不是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 说明1:原点未正确设置

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. 如果这不起作用,请在服务器端输出请求源,并将“localhost”替换为此源。 (I don't know enough node to tell you how to output the origin). (我不知道足够的节点告诉你如何输出原点)。

Explanation 2: Headers not outputted correctly 说明2:标题未正确输出

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). 是您需要设置的列表 (对于PHP,但标题是相同的)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node.js中的请求资源上没有“ Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource in Node.js 无法在NODE.JS中解决此问题,请求的资源在(nodejs + angularjs)中不存在“ Access-Control-Allow-Origin”标头 - Unable to solve this in NODE.JS, No 'Access-Control-Allow-Origin' header is present on the requested resource in (nodejs + angularjs) Node.js:请求的资源上不存在“Access-Control-Allow-Origin”header - Node.js: No 'Access-Control-Allow-Origin' header is present on the requested resource Node.JS/Fetch:请求的资源上不存在“Access-Control-Allow-Origin”header - Node.JS/Fetch: No 'Access-Control-Allow-Origin' header is present on the requested resource Node js Heroku 请求的资源上不存在“Access-Control-Allow-Origin”标头 - Node js Heroku No 'Access-Control-Allow-Origin' header is present on the requested resource Node.Js 错误“请求中不存在‘Access-Control-Allow-Origin’标头” - Node.Js error “No 'Access-Control-Allow-Origin' header is present on the requested” ReactJS 节点和 Axios:请求的资源上不存在“Access-Control-Allow-Origin”标头 - ReactJS Node and Axios :No 'Access-Control-Allow-Origin' header is present on the requested resource 节点 CORS 策略:请求的资源上不存在“Access-Control-Allow-Origin”header - Node CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource 节点和 Heroku,错误:请求的资源上不存在“Access-Control-Allow-Origin”header - Node and Heroku, error: No 'Access-Control-Allow-Origin' header is present on the requested resource 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 -React / Node API - No 'Access-Control-Allow-Origin' header is present on the requested resource. - React/Node API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM