尝试在 Heroku 上部署 Node.js/Express/Socket.io 应用程序时出现应用程序错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24946673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Application Error when attempting to deploy Node.js/Express/Socket.io application on Heroku
提问by keb
I am fairly new to all of these technologies (including somewhat JavaScript) so you might have to bear with me here.
我对所有这些技术(包括一些 JavaScript)都很陌生,所以你可能不得不在这里忍受我。
I followed the ChatApp tutorial over at Socket.IO docs fairly closely and modified the application to my likings somewhat; however, I don't think I changed much on the side of server interaction and stuff. My problem is no matter what I do, I can't seem to be able to get my application to successfully run on Heroku. I get this error message when trying to load the app:
我非常密切地遵循 Socket.IO 文档中的 ChatApp 教程,并根据我的喜好稍微修改了应用程序;但是,我认为我在服务器交互和其他方面没有太大变化。我的问题是无论我做什么,我似乎都无法让我的应用程序在 Heroku 上成功运行。尝试加载应用程序时收到此错误消息:
Application Error An error occurred in the application and your page could not be served. Please try again in a few moments. If you are the application owner, check your logs for details.
应用程序错误 应用程序发生错误,无法提供您的页面。请稍后重试。如果您是应用程序所有者,请检查您的日志以了解详细信息。
I am not sure if I am missing something obvious or what.
我不确定我是否遗漏了一些明显的东西或什么。
Here is my main index.js file:
这是我的主要 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
索引.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>
Additionally, my package.json file
此外,我的 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:
.gitignore:
node_modules/
回答by hunterloftis
Disclosure: I'm the Node Platform Owner at Heroku
披露:我是 Heroku 的节点平台所有者
First, you should run heroku logsto get logging output.
首先,您应该运行heroku logs以获取日志输出。
Second, do you mean to have commented out listenon your server? Without this, your server won't allow any incoming connections:
其次,你的意思是listen在你的服务器上注释掉了吗?没有这个,您的服务器将不允许任何传入连接:
// http.listen(3000, function(){
// console.log('listening on *:3000');
// });
// http.listen(3000, function(){
// console.log('listening on *:3000');
// });
Finally, instead of binding to a hardcoded port (3000), you should bind to an environment variable with a default:
最后,不要绑定到硬编码端口 (3000),您应该绑定到具有默认值的环境变量:
http.listen(process.env.PORT || 3000, function(){
console.log('listening on', http.address().port);
});
http.listen(process.env.PORT || 3000, function(){
console.log('listening on', http.address().port);
});
回答by Nhiyhe
I encountered the same error, but including "start":"node app.js"in package.json file tends to fix the problem. Hope this helps anyone that encounters the same error.
我遇到了同样的错误,但包含"start":"node app.js"在 package.json 文件中往往可以解决问题。希望这可以帮助遇到相同错误的任何人。
Note:app.js should be your own main server file.
注意:app.js 应该是你自己的主服务器文件。
回答by Clement
After checking heroku logsI was able to find out that my bcrypt dependency defined properly in package.json. I would recommend you:
检查后,heroku logs我能够发现我的 bcrypt 依赖项在 package.json 中正确定义。我会推荐你:
- Check that your dependencies have the right versions attached to them.
- Delete node_modules file
- Run
npm install - Check if all the dependencies installed properly
- If they have installed properly, then
git push heroku
- 检查您的依赖项是否附加了正确的版本。
- 删除 node_modules 文件
- 跑
npm install - 检查所有依赖项是否安装正确
- 如果他们已经正确安装,那么
git push heroku
回答by Fakuade Segun
Check all your dependency. You could confirm it by cross checking your package.json or rather do refresh installation of all the dependencies in your package.json
检查您的所有依赖项。您可以通过交叉检查您的 package.json 来确认它,或者更确切地说是在您的 package.json 中刷新安装所有依赖项
回答by Grid Trekkor
I also encountered this error after not using my Node app for several weeks. The reason appeared to be that not only had the app gone to sleep, but the database and its connection had too. I'm using a free MongoDB instance hosted by MongoLab.
在几个星期没有使用我的 Node 应用程序后,我也遇到了这个错误。原因似乎是不仅应用程序进入睡眠状态,而且数据库及其连接也进入睡眠状态。我正在使用由 MongoLab 托管的免费 MongoDB 实例。
I fixed this by running my local copy of the app, causing MongoLab to awaken. Then after a few minutes, my Heroku-hosted app started working again.
我通过运行应用程序的本地副本解决了这个问题,导致 MongoLab 唤醒。几分钟后,我的 Heroku 托管应用程序再次开始工作。

