【问题标题】:Mqtt Node Js deploying on heroku crashes部署在 Heroku 上的 Mqtt Node Js 崩溃
【发布时间】:2015-12-25 18:14:57
【问题描述】:

我正在做一个物联网项目。我将使用 Mqtt 进行设备之间的通信,我开始使用 NodeJs 和mosca 运行简单的示例,它在我的本地 linux 机器上运行。

当我开始在 Heroku 上进行部署时,我遇到了崩溃问题。

这是我的代码:

var mosca = require('mosca')
var http = require('http');
var url = require('url');
var sys = require('sys');

var settings = {
  port: 1883 || Number(process.env.PORT)
};

//here we start mosca
var server = new mosca.Server(settings);
server.on('ready', setup);

// fired when the mqtt server is ready
function setup() {
  console.log('Mosca server is up and running')
}

// fired whena  client is connected
server.on('clientConnected', function(client) {
  console.log('client connected', client.id);
});

// fired when a message is received
server.on('published', function(packet, client) {
  console.log('Published : ', packet.payload);
});

// fired when a client subscribes to a topic
server.on('subscribed', function(topic, client) {
  console.log('subscribed : ', topic);
});

// fired when a client subscribes to a topic
server.on('unsubscribed', function(topic, client) {
  console.log('unsubscribed : ', topic);
});

// fired when a client is disconnecting
server.on('clientDisconnecting', function(client) {
  console.log('clientDisconnecting : ', client.id);
});

// fired when a client is disconnected
server.on('clientDisconnected', function(client) {
  console.log('clientDisconnected : ', client.id);
});

这是heroku日志报告的崩溃:

State changed from crashed to starting
2015-09-28T12:49:43.209288+00:00 heroku[web.1]: Starting process with      command `node index.js`
2015-09-28T12:49:48.669918+00:00 app[web.1]: (node) sys is deprecated.  Use util instead.
2015-09-28T12:49:48.718428+00:00 app[web.1]: Mosca server is up and  running
2015-09-28T12:50:43.468112+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of  launch
2015-09-28T12:50:43.468112+00:00 heroku[web.1]: Stopping process with SIGKILL
2015-09-28T12:50:44.373307+00:00 heroku[web.1]: State changed from starting to crashed
2015-09-28T12:50:44.352908+00:00 heroku[web.1]: Process exited with status 137
2015-09-28T12:53:30.899810+00:00 heroku[slug-compiler]: Slug compilation started
2015-09-28T12:53:30.899837+00:00 heroku[slug-compiler]: Slug compilation finished
2015-09-28T12:53:30.830161+00:00 heroku[api]: Deploy c237cb9 by mstfkhattab@gmail.com
2015-09-28T12:53:30.830200+00:00 heroku[api]: Release v20 created by mstfkhattab@gmail.com
2015-09-28T12:53:30.926174+00:00 heroku[web.1]: State changed from crashed to starting
2015-09-28T12:53:33.726073+00:00 heroku[web.1]: Starting process with command `node index.js`
2015-09-28T12:53:36.600467+00:00 app[web.1]: (node) sys is deprecated. Use util instead.
2015-09-28T12:53:36.643097+00:00 app[web.1]: Mosca server is up and running
2015-09-28T12:54:33.858449+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2015-09-28T12:54:33.858449+00:00 heroku[web.1]: Stopping process with SIGKILL
2015-09-28T12:54:34.801910+00:00 heroku[web.1]: State changed from starting to crashed
2015-09-28T12:54:34.781887+00:00 heroku[web.1]: Process exited with status 137
2015-09-28T12:54:36.720413+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=iot-mqtt-glinty-tutorial.herokuapp.com request_id=b74f96e4-0a38-405d-a3c6-38fd41c601b0 fwd="196.221.206.12" dyno= connect= service= status=503 bytes=
2015-09-28T12:54:37.904668+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=iot-mqtt-glinty-tutorial.herokuapp.com request_id=19d27d3a-a478-47a0-8e3f-532d741d15ac fwd="196.221.206.12" dyno= connect= service= status=503 bytes=

有什么解决办法吗???

【问题讨论】:

    标签: node.js heroku port mqtt


    【解决方案1】:

    它失败了,因为它期望一个 Web 进程绑定到 process.env.PORT,但是没有这样的进程。您包含 http 模块但从不使用它。默认情况下,Heroku 期望运行某种 Web 服务。

    如果此应用程序没有 Web 进程(不应侦听端口),那么您应该创建一个 Procfile 并指定您希望如何启动它,例如:

    server: node server.js
    

    有关 Procfile 的更多信息:

    【讨论】:

    • 看来Procfile在heroku中的作用很大,但问题是heroku总是在索引文件中搜索默认的http/webservice
    • Heroku 不认为 'index' 有什么特别之处。唯一特别的是在没有 Procfile 的情况下假设“网络”服务。在没有向 Heroku 指示如何启动您想要启动的进程的情况下,Heroku 还会做些什么来启动您的应用程序?即,如果您不告诉 Heroku 如何启动您的应用程序,您希望应用程序启动系统做什么?
    猜你喜欢
    • 1970-01-01
    • 2012-10-02
    • 1970-01-01
    • 2019-05-21
    • 2022-10-07
    • 2016-04-23
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    相关资源
    最近更新 更多