【问题标题】:My socket.io app's server is not starting on Heroku我的 socket.io 应用程序的服务器没有在 Heroku 上启动
【发布时间】:2014-09-04 08:23:17
【问题描述】:

我正在尝试在 Heroku 上部署我的应用程序。我已按照以下说明进行操作:https://devcenter.heroku.com/articles/getting-started-with-nodejs,启用 websockets,但是当我运行 $heroku open 时,我进入了错误页面。

这是我从 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

你知道我应该改变什么来让应用正常工作吗?如果您需要我提供更多信息,请告诉我。

谢谢!

【问题讨论】:

    标签: node.js heroku deployment socket.io


    【解决方案1】:

    您的代码正在尝试绑定到硬编码端口 8080,但 Heroku will tell your app which port to connect to。 Heroku 使用它来检测您的应用程序是否正确启动,以及用于路由。但是,您的代码并未在该端口上启动,因此它会因错误“Web 进程未能在启动后 60 秒内绑定到 $PORT”而被终止。 Heroku 不知道您的应用程序已经启动,也不知道它正在侦听哪个端口。

    不过,这很容易解决。这将告诉您的应用程序连接到PORT 环境变量中指定的端口,如果未设置该变量,则为 8080:

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

    【讨论】:

      猜你喜欢
      • 2013-11-14
      • 2016-02-11
      • 2017-06-12
      • 1970-01-01
      • 2013-07-21
      • 2014-11-22
      • 2018-06-28
      • 2021-08-19
      • 2011-03-31
      相关资源
      最近更新 更多