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

huangapple go评论75阅读模式
英文:

How to stop a random node error: '(node:196569) Warning: An error event has already been emitted on the socket.'

问题

当我运行我的Node.js Express Web服务器时,最终在终端中出现以下错误:

(node:196569) 警告: 套接字上已经发出错误事件。请在处理 'clientError' 事件时使用套接字的 destroy 方法。
(使用 `node --trace-warnings ...` 来显示警告的创建位置)

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

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

// 设置 Express
const app = express();

// 设置 SSL 证书
const https_options = {
    key: process.env.HUTCHYBOP_KEY.replace(/\n/g, '\n'),
    cert: process.env.CRT_CODE.replace(/\n/g, '\n'),
    keepAlive: false
};

... 我的其他代码路由等...

// HTTP 服务器(端口 80,应该重定向到 HTTPS)
app.listen(80);

// HTTPS 服务器(端口 443)
https.createServer(https_options, app).listen(443, () => {
    console.log('服务器正在监听端口 443(https)');
    serveHTTP() // 记录函数
});

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

我添加了 --trace-warnings 选项并得到了以下输出:

(node:230020) 警告: 套接字上已经发出错误事件。请在处理 'clientError' 事件时使用套接字的 destroy 方法。
    在 warnUnclosedSocket (node:_http_server:827:11)
    在 TLSSocket.socketOnError (node:_http_server:841:5)
    在 onParserExecuteCommon (node:_http_server:876:19)
    在 onParserExecute (node:_http_server:797:3)

我已经在网上搜索了解如何解决此错误的解决方案,但没有找到有效的方法。我相对新手,现在不知道下一步该怎么做。

有人知道为什么会发生这个错误以及如何停止它吗?

英文:

I have an express web server built in node.js. When I run my server, eventually I get the following error in the terminal:

(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)

I am using node:https to create the server as follows:

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
});

After doing some reading, I added the keepAlive option hoping it might solve the issue, it did not.

I have added the --trace-warnings option and got:

(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)

I have had a google round for solutions to the error but have come up short and I'm fairly new to this so I'm now stuggling to work out what to do next.

Does anybody know why the error is happing and how to stop it?

答案1

得分: 1

我也在使用 Vite 项目与通过 Docker 与 API 服务器通信时遇到了相同的错误。问题在于我的 .env 文件中的 API 地址是错误的。一旦我将变量更改为正确的数值/IP 地址,问题就解决了。你的应用程序是否在初始化之前发出请求?如果是的话,你可能需要检查你正在请求的地址。如果问题仍然存在,我建议:

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

I also got the same error with a vite project that's communicating with an api server through docker. The problem was that the IP address of the api in my .env was wrong. It is fixed, once I changed my variable with the correct value/ IP. Does your app somehow make a request prior to the initiation of your app? If yes, you may want to check the address you are requesting to. If the problem still occurs, I would suggest to:

  1. Delete node_modules
  2. Run npm cache verify to make sure there are no other problems.
  3. Run npm install

huangapple
  • 本文由 发表于 2023年7月10日 17:36:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定