I'm using the following event emitter (and I need this functionality). My question is how to avoid the error.
It seems that I'm using event inside another event.
warning: possible EventEmitter memory leak detected. 11 AppUp listeners added. Use emitter.setMaxListeners() to increase limit.
I'm using node 0.12.7
This is the all module code:
var events = require('events');
var eventEmitter = new events.EventEmitter();
var run = function (req, res) {
host = req.headers.host.split(':')[0];
proxy.web(req, res, {
target: 'http://' + host + ':' + port
});
};
var runApp = function (req, res) {
appStatus.eventEmitter.on('AppUp', function () {
run(req, res);
});
if (model.get()) {
run(req, res);
}
}
module.exports = {
runApp: runApp
};
Is there a way to avoid this error?
Every time you call the runApp function it sets up a listener for the AppUp event. So if you call runApp function multiple times the run function will be called the same amount of times you called runApp previously plus the actual call when the AppUp event is emitted.
Possible solution is to listen AppUP event only once with the following modification:
appStatus.eventEmitter.once('AppUp', function () {
run(req, res);
});