CORS与socket.io
我在使用express.io的node.js应用程序的CORS上遇到了问题。 我是远程托pipe的socket.io客户端JS,因为这需要作为一个远程应用程序。
<script src="resources/js/socket.io.min.js"></script>
它在OpenShift上托pipe
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运行客户端与本地服务器:8080 socket.io工作正常。
当我从localhost:8888和odechat-latestsightings.rhcloud.com上的服务器运行客户端时,socket.io超时:
Firebug:GET http://nodechat-latestsightings.rhcloud.com:8888/socket.io/1/?t=1391542144169 1分16秒
其他路线工作正常:GET http://nodechat-latestsightings.rhcloud.com/rooms 200 OK 664ms
我只是无法弄清楚这一点
http://nodechat-latestsightings.rhcloud.com/socket.io/1/?t=1391542144169似乎工作正常
也就是说,没有在URL中的端口号,你绑定的端口是什么?
另外,如果你的服务器在openshift那么你应该绑定到端口:8000使用websockets,因为这是端口openshift的nginx反向代理具有公共访问。
你可以从这里阅读更多: https : //developers.openshift.com/en/managing-port-binding-routing.html
- 在ExpressJS中接受CORS请求的自定义HttpHeaders
- 使用Angular vs Node.js获取图像尺寸
- 来自React的快速POST请求返回空主体
- 获得CORS与JQuery客户端,Node / Express服务器一起工作的问题
- 请求的资源上没有“Access-Control-Allow-Origin”标题。 NodeJS和AngularJS应用程序
- 允许CORS REST请求到Heroku上的Express / Node.js应用程序
- 如何使angular2在成功的预检请求之后正确地触发正确的callback
- CORS麻烦与nodejs和AngularJS
- Express CORS不起作用