CORS与socket.io
我在使用express.io的node.js应用程序上遇到CORS问题。我正在远程托管socket.io客户端js,因为这需要作为远程应用程序。
<script src="resources/js/socket.io.min.js"></script>
它托管在OpenShift上
server.js:
var ipaddr = process.env.OPENSHIFT_NODEJS_IP || "localhost";
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var express = require('express.io');
// magical express.io
var app = express();
// Enables CORS
var enableCORS = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With, *');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
} else {
next();
};
};
app.configure(function() {
// enable CORS!
app.use(enableCORS);
});
app.http().io();
app.io.set('origins', '*:*');
//.... other stuff
app.listen(port, ipaddr);
然后在客户端:
var socket = io.connect(window.chat_url);
当我从localhost:8888运行客户端与服务器localhost:8080 socket.io工作正常。
当我从localhost:8888和odechat-latestsightings.rhcloud上的服务器运行客户端时,socket.io超时:
Firebug:GET :8888/socket.io/1/?t=1391542144169 1m 16s
其他路线工作正常:GET 200 OK 664ms
我只是想不出来
回答如下:http://nodechat-latestsightings.rhcloud/socket.io/1/?t=1391542144169似乎工作正常
如果没有url中的端口号,你绑定的端口是什么?