简体   繁体   English

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

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

I have this simple Express.js back-end server:我有这个简单的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 + "!"));

But I keep getting this warning and I actually have no idea about what is the reason for it.但我不断收到这个警告,我实际上不知道它的原因是什么。 I don't have web sockets or something, but still:我没有网络套接字或其他东西,但仍然:

(node:7847) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 
11 uncaughtException listeners added to [process]. 
Use emitter.setMaxListeners() to increase limit

That specific error means that your code (or the modules you are using) have added 11 handlers for the process.on('uncaughtException', ...) event.该特定错误意味着您的代码(或您正在使用的模块)为process.on('uncaughtException', ...)事件添加了 11 个处理程序。

This is typically an indication that some code is repeatedly adding the same event handler over and over again (in some other event handler such as inside an Express route) where they accumulate more and more forever.这通常表明某些代码一遍又一遍地重复添加相同的事件处理程序(在某些其他事件处理程序中,例如在 Express 路由中),它们会越来越多地累积。 This could be in either your own code or in some module that your code imports.这可能在您自己的代码中,也可能在您的代码导入的某些模块中。

Event handlers like this should be added only once for a given section of code or should be added, then removed when no longer needed.像这样的事件处理程序应该只为给定的代码部分添加一次,或者应该添加,然后在不再需要时删除。

Based on the small amount of code you show here, I'd guess that the first place to look would be in ./src/routes for any code that adds an uncaughtException listener in one of your route handlers.根据您在此处显示的少量代码,我猜首先要查看的位置是./src/routes ,以查找在您的一个路由处理程序中添加uncaughtException侦听器的任何代码。

I have found the reason of this warning.我找到了这个警告的原因。

In this project I use winston and winston-daily-rotate-file , for every controller and service (to write down logs) I create this logger instance, and basically this creates those handlers:在这个项目中,我使用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;

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

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