Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试在Heroku上部署Node.js/Express/Socket.io应用程序时出现应用程序错误_Node.js_Heroku_Express_Socket.io - Fatal编程技术网

尝试在Heroku上部署Node.js/Express/Socket.io应用程序时出现应用程序错误

尝试在Heroku上部署Node.js/Express/Socket.io应用程序时出现应用程序错误,node.js,heroku,express,socket.io,Node.js,Heroku,Express,Socket.io,我对所有这些技术(包括一些JavaScript)都相当陌生,所以您可能不得不在这里接受我的建议 我在Socket.IO文档上非常仔细地学习了ChatApp教程,并根据自己的喜好对应用程序进行了一些修改;然而,我认为我在服务器交互和其他方面没有太大的改变。我的问题是,无论我做什么,我似乎都无法让我的应用程序在Heroku上成功运行。我在尝试加载应用程序时收到以下错误消息: 应用程序错误 应用程序中出现错误,无法提供您的页面。请稍后再试。如果您是应用程序所有者,请查看日志以了解详细信息 我不确定我是

我对所有这些技术(包括一些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>
程序文件:

web: node index.js
.gitignore:

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

首先,您应该运行heroku日志以获得日志输出

第二,你的意思是在你的服务器上注释掉
listen
?否则,服务器将不允许任何传入连接:


//http.listen(3000,函数(){
//console.log('监听*:3000');
// });

最后,您不应绑定到硬编码端口(3000),而应使用默认值绑定到环境变量:


http.listen(process.env.PORT | | 3000,函数(){
log('监听',http.address().port);
});

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


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

在几个星期没有使用我的节点应用程序后,我也遇到了这个错误。原因似乎是,不仅应用程序进入睡眠状态,数据库及其连接也进入睡眠状态。我正在使用MongoDB托管的免费MongoDB实例


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

在检查了Heroku日志后,我发现我的bcrypt依赖项在package.json中定义正确。我建议你:

  • 检查您的依赖项是否附加了正确的版本
  • 删除节点模块文件
  • 运行
    npm安装
  • 检查是否正确安装了所有依赖项
  • 如果它们安装正确,则
    git-push-heroku

  • 检查所有依赖项。
    您可以通过交叉检查package.json来确认这一点,或者更确切地说是刷新包中所有依赖项的安装。json

    heroku日志
    帮助我发现express安装在devdependences中,而不是依赖项中。我在dependencies中安装了它,它成功了!谢谢这太疯狂了,我失去了所有的模块。同样,我需要安装
    node_modules/