如何停止随机节点错误:“(节点:196569)警告:套接字上已发出错误事件。”

hut*_*Bop 6 https node.js express

我有一个用 Node.js 构建的 Express Web 服务器。当我运行服务器时,最终我在终端中收到以下错误:

(node:196569) Warning: An error event has already been emitted on the socket. Please use the destroy method on the socket while handling a 'clientError' event.
(Use `node --trace-warnings ...` to show where the warning was created)
Run Code Online (Sandbox Code Playgroud)

我正在使用 node:https 创建服务器,如下所示:

const express = require('express');
const https = require('node:https');

// setting up express
const app = express();

// Setting up SSL Certificates
const https_options = {
    key: process.env.HUTCHYBOP_KEY.replace(/\\n/g, '\n'),
    cert: process.env.CRT_CODE.replace(/\\n/g, '\n'),
    keepAlive: false
};

... rest of my code, routes ect...


// http server (Port 80, should redirect to https);
app.listen(80);

// https Server (Port 443)
https.createServer(https_options, app).listen(443, () => {
    console.log('Server listening on PORT 443 (https)');
    serveHTTP() // logging function
});
Run Code Online (Sandbox Code Playgroud)

经过一些阅读后,我添加了 keepAlive 选项,希望它可以解决问题,但它没有。

我添加了 --trace-warnings 选项并得到:

(node:230020) Warning: An error event has already been emitted on the socket. Please use the destroy method on the socket while handling a 'clientError' event.
    at warnUnclosedSocket (node:_http_server:827:11)
    at TLSSocket.socketOnError (node:_http_server:841:5)
    at onParserExecuteCommon (node:_http_server:876:19)
    at onParserExecute (node:_http_server:797:3)
Run Code Online (Sandbox Code Playgroud)

我已经在谷歌上搜索了该错误的解决方案,但效果不佳,而且我对此还很陌生,所以我现在正在努力找出下一步该做什么。

有谁知道为什么会发生错误以及如何阻止它?

小智 6

我在通过 docker 与 api 服务器通信的 vite 项目中也遇到了同样的错误。问题是我的 .env 中 api 的 IP 地址是错误的。一旦我用正确的值/IP 更改了变量,它就被修复了。您的应用程序是否在启动应用程序之前以某种方式发出请求?如果是,您可能需要检查您请求的地址。如果问题仍然出现,我建议:

  1. 删除node_modules
  2. 运行npm cache verify以确保没有其他问题。
  3. 跑步npm install