[英]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: 这个问题可能有几种可能的解释:
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).
(我不知道足够的节点告诉你如何输出原点)。
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.