【问题标题】:Application Error when attempting to deploy Node.js/Express/Socket.io application on Heroku尝试在 Heroku 上部署 Node.js/Express/Socket.io 应用程序时出现应用程序错误
【发布时间】:2014-09-16 18:34:00
【问题描述】:

我对所有这些技术(包括一些 JavaScript)都很陌生,所以你可能不得不在这里忍受我。

我非常密切地按照 Socket.IO 文档中的 ChatApp 教程进行操作,并根据自己的喜好对应用程序进行了一些修改;但是,我认为我在服务器交互和其他方面没有太大变化。我的问题是无论我做什么,我似乎都无法让我的应用程序在 Heroku 上成功运行。尝试加载应用程序时收到此错误消息:

应用程序错误 应用程序发生错误,无法提供您的页面。请稍后重试。如果您是应用程序所有者,请查看您的日志以了解详细信息。

我不确定我是否遗漏了一些明显的东西或什么。

这是我的主要 index.js 文件:

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

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

app.use("/css", express.static(__dirname + '/css'));

//array of users currently in chat
var people = {};

io.on('connection', function(socket){
    console.log('user connected!');

    socket.on('join', function(name){
        people[socket.id] = name; //create entry in 'people' with new user
        socket.emit("update", "You have connected to the server.");
        io.sockets.emit("update", name + " has joined the server.");
        io.sockets.emit("update_people_list", people);
    });

    socket.on('disconnect', function(){
        console.log('user disconnected!');
        if(people[socket.id] != ""){
            io.sockets.emit("update", people[socket.id] + " has left the server.");
            delete people[socket.id];
            io.sockets.emit("update_people_list", people);
        }
    });

    socket.on('chat message', function(msg){
        console.log('message: ' + msg);
        io.sockets.emit('chat message', people[socket.id], msg);
    });
});


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

index.html

<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery.js"></script>
<script>
  $(document).ready(function(){
    var ready = false;
    var socket = io.connect();

    $("#chat").hide();
    $(".canvasDiv").hide();
    $("#name").focus();
    //prevent form from being submitted without name
    $("form").submit(function(event){
      event.preventDefault();
    });

    //allows entering by hitting 'Enter' for name
    $("#name").keypress(function(e){
      if(e.which == 13) { //if ENTER key
        var name = $("#name").val();
        if(name != ""){
          socket.emit("join", name);
          $("#login").detach();
          $("#chat").show();
          $("#msg").focus();
          ready = true;
        }
      }
    });

    $('#chatform').submit(function(){  //when submit chat message
      socket.emit('chat message', $('#msg').val()); //emit message + value of input
      $('#msg').val('');  //empty field?
      return false; //so that the page doesn't refresh
    });

    //SOCKET LISTENING
    socket.on('chat message', function(user, msg){
      if(ready){
        $('#messages').append("<p><strong><span class='chat-user'>" + htmlEntities(user) + " </span></strong>:  " + htmlEntities(msg) + "</p>");
        //adjust height and scroll as need be:
        var $container = $('#messages');
        var height = $container.get(0).scrollHeight;
        $container.scrollTop(height);
      }
    });

    socket.on("update", function(io_message){
      if(ready){
        $('#messages').append("<p class='notification'>" + htmlEntities(io_message) + "</p>")
      }
    });

    socket.on("update_people_list", function(people){
      if(ready){
        $("#people").empty(); //clear to refresh it
        $.each(people, function(client_id, name){
          $('#people').append("<p class='notification'>" + htmlEntities(name) + "</p>");
        });
        var $container = $("#messages");
        var height = $container.get(0).scrollHeight;
        $container.scrollTop(height);
      }
    });

  });
</script>

另外,我的 package.json 文件

 {
  "name": "socket-chat-example",
  "version": "0.0.1",
  "description": "my first socket.io app",
  "dependencies": {
    "express": "^4.6.1",
    "socket.io": "^1.0.6"
  }
}

过程文件:

web: node index.js

.gitignore:

node_modules/

【问题讨论】:

    标签: node.js heroku express socket.io


    【解决方案1】:

    披露:我是 Heroku 的节点平台所有者

    首先,您应该运行heroku logs 以获取日志输出。

    第二,你的意思是在你的服务器上注释掉了listen?没有这个,您的服务器将不允许任何传入连接:

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

    最后,您应该绑定到具有默认值的环境变量,而不是绑定到硬编码端口 (3000):

    http.listen(process.env.PORT || 3000, function(){ console.log('listening on', http.address().port); });

    【讨论】:

    • heroku logs 帮助我发现 express 安装在 devDependencies 而不是依赖项中。我将它安装在依赖项中并且它有效!谢谢!
    【解决方案2】:

    我遇到了同样的错误,但在 package.json 文件中包含 "start":"node app.js" 往往可以解决问题。希望这可以帮助遇到相同错误的任何人。

    注意: app.js 应该是您自己的主服务器文件。

    【讨论】:

      【解决方案3】:

      在几周未使用我的 Node 应用程序后,我也遇到了这个错误。原因似乎是不仅应用程序进入了睡眠状态,而且数据库及其连接也进入了睡眠状态。我正在使用由 MongoLab 托管的免费 MongoDB 实例。

      我通过运行应用程序的本地副本解决了这个问题,导致 MongoLab 被唤醒。几分钟后,我的 Heroku 托管应用又开始工作了。

      【讨论】:

        【解决方案4】:

        检查heroku logs 后,我发现我的 bcrypt 依赖项在 package.json 中正确定义。我会推荐你​​:

        1. 检查您的依赖项是否附加了正确的版本。
        2. 删除 node_modules 文件
        3. 运行npm install
        4. 检查所有依赖是否安装正确
        5. 如果它们安装正确,那么git push heroku

        【讨论】:

        • 这太疯狂了,我丢失了所有模块。同样,我需要安装
        【解决方案5】:

        检查所有依赖项。 您可以通过交叉检查您的 package.json 来确认它,或者刷新您 package.json 中所有依赖项的安装

        【讨论】:

          猜你喜欢
          • 2018-06-19
          • 2017-02-04
          • 2020-03-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-30
          • 1970-01-01
          • 2021-03-25
          相关资源
          最近更新 更多