node.js - 请求 - 如何“emitter.setMaxListeners()”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8313628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
node.js - request - How to "emitter.setMaxListeners()"?
提问by camden_kid
When I do a GET on a certain URI using the node.js 'request' module;
当我使用 node.js 'request' 模块对某个 URI 执行 GET 时;
var options = {uri:"aURI", headers:headerData};
request.get(options, function (error, response, body) {
}
The error message is:
错误信息是:
[Error: Exceeded maxRedirects. Probably stuck in a redirect loop.]
and there is also the following message:
还有以下消息:
"(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit."
How do I setMaxListeners?
我怎么办setMaxListeners?
回答by Félix Brunet
I strongly advice NOT to use the code:
我强烈建议不要使用代码:
process.setMaxListeners(0);
The warning is not there without reason. Most of the time, it is because there is an error hidden in your code. Removing the limit removes the warning, but not its cause, and prevents you from being warned of a source of resource leakage.
警告不是没有理由的。大多数情况下,这是因为您的代码中隐藏了错误。删除限制会删除警告,但不会删除其原因,并防止您收到资源泄漏源的警告。
If you hit the limit for a legitimate reason, put a reasonable value in the function (the default is 10).
如果出于合法原因达到限制,请在函数中放置一个合理的值(默认值为 10)。
Also, to change the default, it is not necessary to mess with the EventEmitter prototype. you can set the value of defaultMaxListeners attribute like so:
此外,要更改默认值,没有必要弄乱 EventEmitter 原型。您可以像这样设置 defaultMaxListeners 属性的值:
require('events').EventEmitter.defaultMaxListeners = 15;
回答by zag2art
I use the code to increase the default limit globally:
require('events').EventEmitter.prototype._maxListeners = 100;
我使用代码全局增加默认限制:
require('events').EventEmitter.prototype._maxListeners = 100;
回答by camden_kid
This is how I solved the problem:
我是这样解决问题的:
In main.js of the 'request' module I added one line:
在“请求”模块的 main.js 中,我添加了一行:
Request.prototype.request = function () {
var self = this
self.setMaxListeners(0); // Added line
This defines unlimited listeners http://nodejs.org/docs/v0.4.7/api/events.html#emitter.setMaxListeners
这定义了无限的侦听器http://nodejs.org/docs/v0.4.7/api/events.html#emitter.setMaxListeners
In my code I set the 'maxRedirects' value explicitly:
在我的代码中,我明确设置了 'maxRedirects' 值:
var options = {uri:headingUri, headers:headerData, maxRedirects:100};
回答by Mariya Davydova
Although adding something to nodejs module is possible, it seems to be not the best way (if you try to run your code on other computer, the program will crash with the same error, obviously).
虽然向 nodejs 模块添加一些东西是可能的,但这似乎不是最好的方法(如果你尝试在其他计算机上运行你的代码,程序显然会因为同样的错误而崩溃)。
I would rather set max listeners number in your own code:
我宁愿在您自己的代码中设置最大听众数:
var options = {uri:headingUri, headers:headerData, maxRedirects:100};
request.setMaxListeners(0);
request.get(options, function (error, response, body) {
}
回答by Malik Jehangir Riaz
Try to use: require('events').EventEmitter.defaultMaxListeners = Infinity;
尝试使用: require('events').EventEmitter.defaultMaxListeners = Infinity;
回答by vijay
this is Extension to @Félix Brunet answer
这是@Félix Brunet答案的扩展
Reason- there is code hiddenin your app
原因-您的应用程序中隐藏了代码
How to find -
怎么找 -
- Strip/comment code and execute until you reach error
- check log file
- 剥离/注释代码并执行,直到出现错误
- 检查日志文件
Eg- In my case i created 30 instances of winston logUnknowinglyand it started giving error
例如- 在我的情况下,我在不知不觉中创建了 30 个winston log实例,它开始出现错误
Note :if u supress this error , it will come again afetr 3..4 days
注意:如果你抑制这个错误,它会在 3..4 天后再次出现

