1

I make https request with Node's https.request() and listen to response 'data' event:

const req = https.request('https://stackoverflow.com/', res => {
    res.on('data', (chunk) => {
        console.log('data');
    })
});

req.end();

I works fine. I get 'data' outputs in console.

However, when I add hook to reqest's emit (for debug purposes) as this:

const req = https.request('https://stackoverflow.com/', res => {
    res.on('data', (chunk) => {
        console.log('data');
    })
});
const original_emit = req.emit;
req.emit = function(event_name) {
    console.log('reqest event: ' + event_name);
    original_emit.apply(req, arguments);
};

req.end();

I don't get 'data' output anymore. Cannot find reason for that.

Node version: 8.11.3

Arbaz Siddiqui
  • 441
  • 2
  • 10
Alex Velickiy
  • 541
  • 1
  • 7
  • 22
  • I think you need to monkeypatch the request prototype before calling `request`. However, why monkeypatch at all? Why not attach 2 listeners? – zero298 Oct 05 '18 at 13:05
  • @zero298 Sorry, don't understand your solution. Buy anyway, I'm wondering why these weird things happen with my solution. – Alex Velickiy Oct 05 '18 at 16:37

0 Answers0