【问题标题】:Nodejs - TypeError: Cannot read property 'headersSent' of undefinedNodejs - TypeError:无法读取未定义的属性“headersSent”
【发布时间】: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"
  }
}

【问题讨论】:

    标签: node.js express socket.io


    【解决方案1】:

    关注这个,对你有帮助 您使用过类似

    var express = require("express")();

    的快递

    请替换为:

    var express = require('express')

    【讨论】:

      【解决方案2】:

      如果您想要更多好处,请不要先执行 express 先得到快递如下 const express = require('express')

      让服务器退出它 const app = express();

      您将使用您的服务器来引导应用程序并快速帮助它提供可以位于任何位置的静态文件,例如。公开如下

      
      Now add all it will look as below
      
      const express = require('express');
      const app = express();
      
      app.use(express.static('public'));
      
      app.listen(3000, () => console.log('Its working on port 3000'))```
      

      【讨论】:

        【解决方案3】:

        如果上述情况不起作用,那就试试吧。

        const express = require('express');
        const app = Royexpress();
        

        【讨论】:

          猜你喜欢
          • 2017-03-06
          • 2022-09-23
          • 2022-11-29
          • 2017-08-30
          • 2017-10-11
          • 1970-01-01
          • 2023-03-19
          • 2018-06-02
          • 2020-03-25
          相关资源
          最近更新 更多