【发布时间】:2013-01-09 22:28:05
【问题描述】:
我刚刚将一个 node.js 站点部署到 AppFog。当我转到“http://localhost:8080”时,它在本地运行良好,但是当我到达 AppFog 上的那个时:http://bandwithfriends.aws.af.cm/ 我收到以下 Chrome 控制台错误:
XMLHttpRequest cannot load http://localhost:8080/socket.io/1/?t=1357769454152. Origin http://bandwithfriends.aws.af.cm is not allowed by Access-Control-Allow-Origin.
服务器代码:
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
static = require('node-static'); // for serving files
// This will make all the files in the current folder
// accessible from the web
var fileServer = new static.Server('./');
app.listen(process.env.VCAP_APP_PORT || 8080);
io.configure('development', function(){
io.set('transports', ['xhr-polling']);
});
客户代码 index.html: 我包括这样的socket.io:
<script src="/socket.io/socket.io.js"></script>
客户代码 script.js:
var url = 'http://localhost:8080';
var socket = io.connect(url);
我该如何解决这个问题?
【问题讨论】:
-
我遇到了完全相同的问题,
var app = require('http').createServer(handler),中的handler是什么我尝试使用您的代码但未定义 -
确保包含处理函数: function handler (request, response) { request.addListener('end', function () { fileServer.serve(request, response); // 这将返回正确的文件}); }