gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 23:13:16 24 4
gpt4 key购买 nike

我对所有这些技术(包括一些 JavaScript)都很陌生,所以你可能不得不在这里忍受我。

我非常密切地按照 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.

我不确定我是否遗漏了一些明显的东西或什么。

这是我的主要 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"
}
}

过程文件:

web: node index.js

.gitignore:

node_modules/

最佳答案

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

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

第二,你的意思是把你服务器上的listen注释掉了吗?没有这个,您的服务器将不允许任何传入连接:


//http.listen(3000, function(){
//console.log('正在监听 *:3000');
//});

最后,您应该绑定(bind)到具有默认值的环境变量,而不是绑定(bind)到硬编码端口 (3000):


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

关于node.js - 尝试在 Heroku 上部署 Node.js/Express/Socket.io 应用程序时出现应用程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24946673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com