最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to end a node.js http request early - Stack Overflow

programmeradmin1浏览0评论

I'm requesting a remote file using an https.request in node.js. I'm not interested in receiving the whole file, I just want what's in the first chunk.

var req = https.request(options, function (res) {
    res.setEncoding('utf8');

    res.on('data', function (d) {
         console.log(d);
         res.pause(); // I want this to end instead of pausing
    });
});

I want to stop receiving the response altogether after the first chunk, but I don't see any close or end methods, only pause and resume. My worry using pause is that a reference to this response will be hanging around indefinitely.

Any ideas?

I'm requesting a remote file using an https.request in node.js. I'm not interested in receiving the whole file, I just want what's in the first chunk.

var req = https.request(options, function (res) {
    res.setEncoding('utf8');

    res.on('data', function (d) {
         console.log(d);
         res.pause(); // I want this to end instead of pausing
    });
});

I want to stop receiving the response altogether after the first chunk, but I don't see any close or end methods, only pause and resume. My worry using pause is that a reference to this response will be hanging around indefinitely.

Any ideas?

Share Improve this question edited Aug 1, 2012 at 14:34 hvgotcodes 120k33 gold badges207 silver badges237 bronze badges asked Aug 1, 2012 at 14:33 Stuart MemoStuart Memo 1,1784 gold badges14 silver badges31 bronze badges 3
  • There is HTTP header called Range. Range: bytes=0-1023 to download first KiBi only, for example. BTW, isn't there res.end()? – Aleksei Zabrodskii Commented Aug 1, 2012 at 14:58
  • 1 Sadly not, res.end() doesn't exist within the callback function. – Stuart Memo Commented Aug 3, 2012 at 14:00
  • stackoverflow.com/questions/33819771/… – Andrew Commented Feb 22, 2021 at 22:58
Add a comment  | 

1 Answer 1

Reset to default 17

Pop this in a file and run it. You might have to adjust to your local google, if you see a 301 redirect answer from google (which is sent as a single chunk, I believe.)

var http = require('http');

var req = http.get("http://www.google.co.za/", function(res) {
  res.setEncoding();
  res.on('data', function(chunk) {
    console.log(chunk.length);
    res.destroy(); //After one run, uncomment this.
  });
});

To see that res.destroy() really works, uncomment it, and the response object will keep emitting events until it closes itself (at which point node will exit this script).

I also experimented with res.emit('end'); instead of the destroy(), but during one of my test runs, it still fired a few additional chunk callbacks. destroy() seems to be a more imminent "end".

The docs for the destroy method are here: http://nodejs.org/api/stream.html#stream_stream_destroy

But you should start reading here: http://nodejs.org/api/http.html#http_http_clientresponse (which states that the response object implements the readable stream interface.)

发布评论

评论列表(0)

  1. 暂无评论