尝试在Heroku上部署Node.js / Express / Socket.io应用程序时出现应用程序错误 [英] Application Error when attempting to deploy Node.js/Express/Socket.io application on Heroku
问题描述
我在Socket上跟着ChatApp教程.IO文件相当密切,并将应用程序修改为我的喜好;然而,我不认为我在服务器交互和东西方面变化很大。我的问题是无论我做什么,我似乎无法让我的应用程序成功运行在Heroku上。尝试加载应用程序时收到此错误消息:
应用程序错误
应用程序发生错误,您的页面可能不服请稍后重试。如果您是应用程序所有者,请查看您的日志以了解详细信息。
我不知道我是否缺少明显的东西或什么。 / p>
这是我的主要index.js文件:
var 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'));
//当前在聊天中的用户数组
var people = {};
io.on('connection',function(socket){
console.log('user connected!');
socket.on('join ',function(name){
people [socket.id] = name; //用新用户创建people中的条目
socket.emit(update,你已连接到服务器。);
io.sockets.emit(update,name +已加入服务器。);
io.sockets.emit(update_people_list,people);
};
socket.on('disconnect',function(){
console.log('user disconnect!');
if(people [socket.id] !=){
io.sockets.emit(update,people [socket.id] +已离开服务器);
删除人[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();
//防止表单被提交没有名称
$(form)。submit(function(event){
event.preventDefault();
});
//允许输入点击Enter作为名称
$(#name)。keypress(function(e){
if(e.which == 13){//如果ENTER键
var name = $(#name)。val();
if(name!=){
socket.emit(join,name);
$(#login ).detach();
$(#chat)。show();
$(#msg)。focus();
ready = true;
($)
$ b 'chat''$('#msg')。val()); //发送消息+输入值
$('#msg')。val ); //空字段?
return false; //所以页面不刷新
});
// SOCKET LISTENING
socket.on('chat message',function(user,msg){
if(ready){
$('#messages ').append(< p>< strong>< span class ='chat-user'>+ htmlEntities(user)+< / span>< / strong> ;:+ htmlEntities )+< / p>);
//根据需要调整高度和滚动:
var $ container = $('#messages');
var height = .get(0).scrollHeight;
$ container.scrollTop(height);
}
});
socket.on(update,function(io_message){
if(ready){
$('#messages')。append(< p class = '通知'>+ htmlEntities(io_message)+< / p>)
}
});
socket.on(update_people_list,function(people){
if(ready){
$(#people)。empty(); //清除刷新它
$ .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我的第一个socket.io应用程序,
依赖:{
express:^ 4.6.1,
socket.io:^ 1.0.6
}
}
Procfile:
web:node index.js
.gitignore :
node_modules /
披露:我是Heroku的Node Platform所有者
,您应该运行 heroku日志
获取日志记录输出。
其次,你是否有意见删除在您的服务器上收听
没有这个,您的服务器将不允许任何传入的连接:
// http.listen(3000,function()
// console.log('listen on *:3000');
//});
最后,不要绑定到硬编码端口(3000),您应该使用默认值绑定到环境变量:
http.listen(process.env.PORT || 3000,function(){
console.log 'listen on',http.address()。port);
});
I am fairly new to all of these technologies (including somewhat JavaScript) so you might have to bear with me here.
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:
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:
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>
Additionally, my package.json file
{
"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/
Disclosure: I'm the Node Platform Owner at Heroku
First, you should run heroku logs
to get logging output.
Second, do you mean to have commented out listen
on your server? Without this, your server won't allow any incoming connections:
// 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:
http.listen(process.env.PORT || 3000, function(){
console.log('listening on', http.address().port);
});
这篇关于尝试在Heroku上部署Node.js / Express / Socket.io应用程序时出现应用程序错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!