NodeJS 如何停止随机节点错误:'(节点:196569)警告:已在套接字上发出错误事件,'

insrf1ej  于 12个月前  发布在  Node.js
关注(0)|答案(2)|浏览(607)

我有一个内置在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)

我使用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
});

在做了一些阅读之后,我添加了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)

我有一个谷歌回合的解决方案的错误,但已经来了短,我是相当新的,所以我现在挣扎着工作下一步该怎么做。
有谁知道为什么会发生错误,以及如何停止它?

k10s72fa

k10s72fa1#

我在一个通过docker与API服务器通信的vite项目中也遇到了同样的错误。问题是我的.env中的API的IP地址是错误的。它是固定的,一旦我改变了我的变量与正确的值/ IP。您的应用是否在启动之前以某种方式发出请求?如果是,您可能需要检查您请求的地址。如果问题仍然存在,我建议:
1.删除node_modules
1.运行npm cache verify以确保没有其他问题。
1.运行npm install

mtb9vblg

mtb9vblg2#

我也遇到了这个问题,看起来我的代码没有任何问题。
该消息已添加到此Node pull请求中:https://github.com/nodejs/node/pull/46769/files
后来有另一个pull request修复了其中的一个bug:https://github.com/nodejs/node/pull/48592/files
我敢打赌,正在修复的bug可能是原因。
该修复已在node v18.8.0中推出。我已经升级了我的节点版本,所以如果我再次看到这个问题,我会尝试记住更新这个答案以排除它。

相关问题