node.js - request - How to “emitter.setMaxListeners()”?

前端 未结 7 1606
忘了有多久
忘了有多久 2020-11-28 06:27

When I do a GET on a certain URI using the node.js \'request\' module;

var options = {uri:\"aURI\", headers:headerData};
request.get(options, function (error         


        
相关标签:
7条回答
  • 2020-11-28 06:35

    This is how I solved the problem:

    In main.js of the 'request' module I added one line:

    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

    In my code I set the 'maxRedirects' value explicitly:

    var options = {uri:headingUri, headers:headerData, maxRedirects:100};
    
    0 讨论(0)
  • 2020-11-28 06:38

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

    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) {
    }
    
    0 讨论(0)
  • 2020-11-28 06:40

    this is Extension to @Félix Brunet answer

    Reason - there is code hidden in 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 log Unknowingly and it started giving error

    Note : if u supress this error , it will come again afetr 3..4 days

    0 讨论(0)
  • 2020-11-28 06:42

    It also happened to me

    I use this code and it worked

    require('events').EventEmitter.defaultMaxListeners = infinity;
    

    Try it out. It may help

    Thanks

    0 讨论(0)
  • 2020-11-28 06:52

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

    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:

    require('events').EventEmitter.defaultMaxListeners = 15;
    
    0 讨论(0)
  • 2020-11-28 06:52

    I use the code to increase the default limit globally: require('events').EventEmitter.prototype._maxListeners = 100;

    0 讨论(0)
提交回复
热议问题