将使用 socket.io 的 node.js 应用程序上传到 Bluemix 会导致 404 错误

Uploading a node.js app that uses socket.io to Bluemix results in 404 error


我刚刚使用 socket.io 创建了节点 js 应用程序
对我来说在本地它工作正常,但是当我将它上传到 bluemix 时,它给我错误

这是在本地主机上运行良好的文件

app.js 文件

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.engine('html', require('swig').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');


app.get('/', function(req, res){
    res.render('index.html');
}); 

io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function() {
    console.log( 'A user disconnected' );
});
socket.on('chat message', function(msg) {
    console.log('Chat message ' + msg);
    var response = {
        'msg': msg,
        'address': socket.request.connection.remoteAddress
    };
    io.emit('chat message', JSON.stringify( response ));
}); 
});

http.listen(1234, '0.0.0.0', function(){
console.log('listening on *:1234');
});

和index.html文件

<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
    * { margin: 0; padding: 0; box-sizing: border-box; }
    body { font: 13px Helvetica, Arial; }
    form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
    form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
    form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
    #messages { list-style-type: none; margin: 0; padding: 0; }
    #messages li { padding: 5px 10px; }
    #messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="/socket.io/socket.io.js"></script>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io();
$('form').submit( function() {
    socket.emit('chat message', $('#m').val());
    $('#m').val('');
    return false;
});
socket.on('chat message', function( info ) {
    var response = JSON.parse( info );
    $('#messages').append($('<li>').text( response.address + ': ' +      response.msg ) );
});
</script>
</body>
</html>

这是服务器上的文件

app.js file

/*eslint-env node*/

//------------------------------------------------------------------
// node.js starter application for Bluemix
//------------------------------------------------------------------

// This application uses express as its web server
// for more info, see: http://expressjs.com
var express = require('express');

// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require('cfenv');

// create a new express server
var app = express();

// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));

// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();

// start server on the specified port and binding host
app.listen(appEnv.port, function() {

// print a message when the server starts listening
console.log("server starting on " + appEnv.url);
});

var http = require('http').Server(app);
var io = require('socket.io')(http);

app.engine('html', require('swig').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');


app.get('/', function(req, res){
    res.render('index.html'); 
});

io.on('connection', function(socket){
    console.log('a user connected');
        socket.on('disconnect', function() {
                    console.log( 'A user disconnected' );
                        });
            socket.on('chat message', function(msg) {
                        console.log('Chat message ' + msg);
                                var response = {
                                                'msg': msg,
                            'address':   socket.request.connection.remoteAddress
                        };
                                        io.emit('chat message',   JSON.stringify( response ));
                                            });
});

index.html 文件相同

我在 google chrome 控制台

上收到此错误
GET http://testingchat.mybluemix.net/socket.io/?EIO=3&transport=polling&t=1440575370819-112 404 (Not Found)

我使用了这个 tutorial

中的代码

谁能帮帮我? 谢谢

您是否在依赖项中将 socket.io 模块添加到 package.json?

"dependencies": {
    "express": "^4.6.1",
    "socket.io": "^1.3.4"
}

此外,尝试从您的终端运行使用 cf logs 命令,以查看当您 运行 应用程序时到底发生了什么。

这是我的 packege.json 文件

{
"name": "NodejsStarterApp",
"version": "0.0.1",
"description": "A sample nodejs app for Bluemix",
"scripts": {
    "start": "node app.js"
},
"dependencies": {
    "express": "4.12.x",
    "cfenv": "1.0.x",
    "swig": "1.x",
    "socket.io": "1.x",
    "socket.io-client": "1.x"
},
"repository": {},
"engines": {
    "node": "0.12.x"
}
}

日志文件中没有错误 错误仅出现在浏览器上

根据您的代码,您设置 socket.io 侦听 1234 端口。 在 Bluemix 运行时上,只会启用端口 80、443 和 9080 进行路由。此外,运行时引擎将动态分配您的本地端口,您必须在设置 socket.io 使用它之前从 vcap 服务读取它。 查看 Bluemix 文档了解如何操作。

关于404错误,是由于对/socket.io/路径的请求