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

对于所有这些技术(包括有些JavaScript),我都相当陌生,因此您可能需要在这里忍受。

我在Socket.IO文档中紧跟着ChatApp教程,并且对应用程序进行了一些修改, 不过,我不认为我在服务器交互和东西方面有很大的改变。 我的问题是不pipe我做什么,我似乎无法让我的应用程序在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" } } 

Procfile:

 web: node index.js 

的.gitignore:

 node_modules/ 

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

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

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

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

最后,不是绑定到一个硬编码的端口(3000),你应该绑定到一个默认的环境variables:

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

在检查heroku logs我能够发现我的bcrypt依赖在package.json中正确定义。 我会build议你:

  1. 检查你的依赖关系是否有正确的版本。
  2. 删除node_modules文件
  3. 运行npm install
  4. 检查是否所有的依赖安装正确
  5. 如果他们已经安装正确,然后git push heroku

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

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

我也没有使用我的Node应用程序几个星期后遇到此错误。 原因似乎是,不仅应用程序已经入睡,而且数据库及其连接也一样。 我正在使用由MongoLab托pipe的免费的MongoDB实例。

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