我有一个简单的Express.js
后端服务器:
const app = require("express")();
const dotenv = require("dotenv");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const swaggerUi = require("swagger-ui-express");
const swaggerJsDocs = require("swagger-jsdoc");
const swaggerOptions = {
definition: {
openapi: '3.0.0',
info: {
title: 'API',
version: '0.0.1',
description: 'Api docs .'
},
},
apis: ["./src/swagger/swagger.yaml"]
};
const swaggerDocs = swaggerJsDocs(swaggerOptions);
dotenv.config();
const server = require("http").createServer(app);
app.use(cookieParser());
app.use(bodyParser.json({limit: '10MB'}));
app.use(bodyParser.urlencoded({extended: true}));
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocs));
app.use("/", require('./src/routes'));
const port = process.env.PORT || 3001;
server.listen(port, () => console.log("API listening on port " + port + "!"));
但我一直收到这样的警告,实际上我不知道是什么原因造成的。我没有网络套接字什么的,但还是:
(node:7847) MaxListenersExceededWarning: Possible EventEmitter memory leak detected.
11 uncaughtException listeners added to [process].
Use emitter.setMaxListeners() to increase limit
发布于 2022-07-17 06:59:56
这个特定的错误意味着您的代码(或您正在使用的模块)为process.on('uncaughtException', ...)
事件添加了11个处理程序。
这通常是一种指示,表明一些代码反复地添加相同的事件处理程序(在其他一些事件处理程序中,例如在Express路由中),它们在这些事件处理程序中积累的时间越来越长。这可以在您自己的代码中,也可以在您的代码导入的某些模块中。
这样的事件处理程序应该只为给定的代码部分添加一次,或者应该添加,然后在不再需要时删除。
根据您在这里显示的少量代码,我猜首先要查看的是./src/routes
中任何在路由处理程序中添加uncaughtException
侦听器的代码。
发布于 2022-07-17 07:17:06
我已经找到了这个警告的原因。
在这个项目中,我使用winston
和winston-daily-rotate-file
,对于每个控制器和服务(记录日志),我创建了这个记录器实例,这基本上创建了这些处理程序:
const loggerInstance = createLogger({
level: process.env.NODE_ENV !== 'production' ? 'debug' : 'info',
format: combine(
colorize(),
json(),
label({ label: data.label ? data.label : 'NONE' }),
timestamp(),
myFormat,
),
transports: transportsConfig,
});
if (process.env.NODE_ENV !== 'production') {
loggerInstance.add(new transports.Console({
handleExceptions: true,
}));
}
if (process.env.DISABLE_LOGGER === 'yes') {
loggerInstance.silent = true;
}
return loggerInstance;
https://stackoverflow.com/questions/73009684
复制相似问题