stop superagent after receiving first byte (to only get redirect url)

I'm using superagent (although willing to user other node lib) with the goal to solely get the redirected url, but not the body. The latter is overkill and I want to prevent my code to download the body if I can help it.

I cannot use HEAD requests since these are not guaranteed to be enabled on the server.

Instead my idea was to pipe the superagent response to a writeStream and stop the writeStream on receiving the first data. However, .on('data', fn) is only available on a readstream instead of a writestream.

Instead I tried to see if superagent.get(...) itself was a readstream on which I could listen for .on('data', fn) to kill the stream, but that doesn't appear to be the case either.

In short, is there another way to cancel a request early, while still getting the redirect url, but not incurring the download overhead of the entire body?


Solved: needed buffer(false) . Using that response can be treated as a proper readstream.

            const url = "http://www.google.com";
            superagent.get(url)
            .buffer(false)
            .end(function (err, res) {
              const urlRedirect = res.redirects.length ? res.redirects.pop() : url;
              res.destroy();
              res.on("end", function () {
                //do stuff with 'urlRedirect'
              })
            })
链接地址: http://www.djcxy.com/p/96724.html

上一篇: 在关闭客户端连接的情况下,Vert.x:正确地流到http响应

下一篇: 在收到第一个字节后停止superagent(仅获取重定向url)