我的socket.io应用程序的服务器不在Heroku上启动

我正在尝试在Heroku上部署我的应用程序。 我已经按照这些说明: https ://devcenter.heroku.com/articles/getting-started-with-nodejs,启用websockets,但是当我运行$ heroku打开,我登陆错误页面。

这里是我从heroku得到的日志:

2014-07-14T11:41:27.331343+00:00 heroku[web.1]: Starting process with command `node index.js` 2014-07-14T11:41:28.431660+00:00 app[web.1]: info: socket.io started 2014-07-14T11:42:27.710153+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch 2014-07-14T11:42:27.710206+00:00 heroku[web.1]: Stopping process with SIGKILL 2014-07-14T11:42:28.915226+00:00 heroku[web.1]: Process exited with status 137 2014-07-14T11:42:28.927884+00:00 heroku[web.1]: State changed from starting to crashed 

这里是我的应用程序的index.js文件:

 // Import the Express module var express = require('express'); // Import the 'path' module (packaged with Node.js) var path = require('path'); // Create a new instance of Express var app = express(); // Import Anagrammatix game file. var agx = require('./game'); // Create a simple Express application app.configure(function() { // Turn down the logging activity app.use(express.logger('dev')); // Serve static html, js, css, and image files from the 'public' directory app.use(express.static(path.join(__dirname,'public'))); }); // Create a Node.js based http server on port 8080 var server = require('http').createServer(app).listen(8080); // Create a Socket.IO server and attach it to the http server var io = require('socket.io').listen(server); // Reduce the logging output of Socket.IO io.set('log level',1); // Listen for Socket.IO Connections. Once connected, start the game logic. io.sockets.on('connection', function (socket) { //console.log('client connected'); agx.initGame(io, socket); }); 

我使用的node_modules是: – express – mysql – socket.io

你知道我应该改变以获得应用程序的工作吗? 让我知道如果你需要我提供更多的信息。

谢谢!

你的代码试图绑定到一个硬编码的端口8080,但Heroku会告诉你的应用程序连接到哪个端口 。 Heroku使用它来检测您的应用程序是否正确启动,以及路由。 但是,你的代码并不是从那个端口开始的,所以它会被错误消息“Web进程无法在启动后的60秒内绑定到$ PORT”。 Heroku不知道你的应用程序已经启动,它不知道它正在监听哪个端口。

虽然这是一个简单的修复。 这将告诉你的应用程序连接到PORT环境variables中指定的PORT ,或者如果该variables未设置,则为8080:

 var server = require('http').createServer(app).listen(process.env.PORT || 8080);