【发布时间】:2021-01-19 00:00:33
【问题描述】:
我正在尝试使用 nodejs、express 和 socket.io 构建一个简单的聊天。但是当我运行我的应用程序时,我在 finalhandler 中收到一个错误。但我什至不知道这是什么。谷歌这次没有提供帮助
我得到的只是一些 github 讨论,对我没有任何帮助
什么是finalhandler?我什至尝试重新启动我的项目,只使用 socketio 和服务器,我得到了同样的错误
app.js:
var express = require("express")();
var http = require("http").Server(app);
var io = require("socket.io")(http);
var path = require("path");
var app = express();
var clients = {};
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res){
res.render('index');
});
io.on("connection", function(client) {
console.log('user connected');
});
io.on("connection", function(client) {
client.on("join", function(name){
console.log("Joined: " + name);
clients[client.id] = name;
client.emit("update", "Você está conectado ao servidor");
client.broadcast.emit("Update! => ", name + "entrou no servidor");
});
client.on("send", function(msg){
console.log("Mensagem: " + msg);
client.broadcast.emit("chat", clients[client.id], msg);
});
client.on("disconnect", function(){
console.log("Disconectado");
io.emmit("Update! => ", clients[client.id] + " saiu do servidor");
delete clients[client.id];
});
});
http.listen(3000, function(){
console.log('Rodando na porta 3000');
});
package.json:
{
"name": "diga",
"version": "1.0.0",
"description": "",
"main": "nodemon",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"serve": "nodemon app.js"
},
"repository": {
"type": "git",
"url": ""
},
"author": "juliana",
"license": "ISC",
"dependencies": {
"@types/socket.io": "^1.4.36",
"ejs": "^2.6.1",
"express": "^4.16.3",
"g": "^2.0.1",
"jquery": "^3.3.1",
"socket.io-client": "^2.1.1",
"socketio": "^1.0.0"
},
"devDependencies": {
"nodemon": "^1.17.5"
}
}
【问题讨论】: