2

I created a simple file downloader using Expressjs 3 and Nodejs 0.10

function download(req, res, filepath, filename){
    res.header('Content-Type', 'application/force-download');
    res.header('Content-Type', 'application/octet-stream');
    res.attachment(filename);
    res.download(filepath, filename);
}

Everything is OK, but when I test application with download managers (for example Internet Download Manager), when I abort or cancel download (before or during download), it throws a warning:

Trace
    at Socket.EventEmitter.addListener (events.js:160:15)
    at Socket.Readable.on (_stream_readable.js:653:33)
    at Socket.EventEmitter.once (events.js:179:8)
    at TCP.onread (net.js:512:26)

(node) warning: <b>possible EventEmitter memory leak detected. 11 listeners added. Use
emitter.setMaxListeners() to increase limit.</b>

How can I remove listener after aborting file download?

Pascal Belloncle
  • 11,184
  • 3
  • 56
  • 56
raminious
  • 161
  • 1
  • 5

1 Answers1

0

The problem is solved
Add setMaxListeners(0) after Expressjs definition:

var express = require('express');

//expressjs 2.x
//var app = module.exports = express.createServer();

//expressjs 3.x
var app = module.exports = express();

//ADD BELOW CODE
app.setMaxListeners(0);


Thanks to @robertklep

Manwal
  • 23,450
  • 12
  • 63
  • 93
raminious
  • 161
  • 1
  • 5
  • This simply removes the limit of listeners that can attach to an `EventEmitter`. The question was about removing event listeners. – nikc.org Aug 10 '13 at 08:23
  • Yeah, you still have the event listeners there, all you did was suppress the warning messages. – jemiloii Jul 19 '16 at 19:50