繁体   English   中英

是什么导致 Node.js “MaxListenersExceededWarning”警告?

[英]What is causing the Node.js "MaxListenersExceededWarning" warning?

我有这个简单的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

该特定错误意味着您的代码(或您正在使用的模块)为process.on('uncaughtException', ...)事件添加了 11 个处理程序。

这通常表明某些代码一遍又一遍地重复添加相同的事件处理程序(在某些其他事件处理程序中,例如在 Express 路由中),它们会越来越多地累积。 这可能在您自己的代码中,也可能在您的代码导入的某些模块中。

像这样的事件处理程序应该只为给定的代码部分添加一次,或者应该添加,然后在不再需要时删除。

根据您在此处显示的少量代码,我猜首先要查看的位置是./src/routes ,以查找在您的一个路由处理程序中添加uncaughtException侦听器的任何代码。

我找到了这个警告的原因。

在这个项目中,我使用winstonwinston-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;

是什么

[英]What is the <!--​ operator in Node.js?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM