我正在尝试在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是: - 表达 - mysql - socket.io
你知道我应该改变什么来让应用程序运行吗?如果您需要我提供更多信息,请告诉我。
谢谢!
答案 0 :(得分:2)
您的代码正在尝试绑定到硬编码端口8080,但是Heroku will tell your app which port to connect to。 Heroku使用它来检测您的应用程序是否正确启动以及路由。但是,您的代码并没有在该端口上启动,因此它会因错误而被杀死#34; Web进程无法在启动后60秒内绑定到$ PORT"。 Heroku不知道您的应用已经启动,并且它不知道它正在收听哪个端口。
但是,这很容易解决。这将告诉您的应用程序连接到PORT
环境变量中指定的端口,如果该变量未设置,则为8080:
var server = require('http').createServer(app).listen(process.env.PORT || 8080);