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

keb*_*keb 11 heroku node.js express socket.io

我对所有这些技术都很陌生(包括一些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');
// });
Run Code Online (Sandbox Code Playgroud)

的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>
Run Code Online (Sandbox Code Playgroud)

另外,我的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"
  }
}
Run Code Online (Sandbox Code Playgroud)

Procfile:

web: node index.js
Run Code Online (Sandbox Code Playgroud)

的.gitignore:

node_modules/
Run Code Online (Sandbox Code Playgroud)

hun*_*tis 31

披露:我是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); });