NodeJS + miekal / request:如何中止请求?

时间:2014-01-15 21:14:55

标签: node.js request streaming pipe

我一直试图中止一个小时的请求,有人可以帮我吗?

这就是我所拥有的:

app.get('/theUrl', function (req, resp) {

  var parser = new Transform();
  parser._transform = function(data, encoding, done) {
    var _this = this;
    // Process data here
  }.on("error", function(err){
    console.log(err);
  });

  var theRequest = request({
    'method' : 'GET',
    'url': '/anotherUrl',
    'headers' : {
      "ACCEPT"  : "text/event-stream"
    }
  }, function (error, response, body){
    if (error) {
      //Throw error
    }
  }).pipe(parser).pipe(resp);

  req.on("close", function(){
    parser.end();
    theRequest.abort(); //Doesn't work
  });
});

正如您所看到的那样,它是一个流媒体代理,因此如果客户端取消请求,我会抓住它并需要关闭或中止转发请求(theRequest)。

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

https://github.com/mikeal/request/issues/772#issuecomment-32480203引用nylen:

来自文档:

  

pipe()返回目标流

因此您不再使用Request对象。在这种情况下,您在ServerResponse上调用abort()。这样做:

var theRequest = request({ ... });
theRequest.pipe(parser);
theRequest.abort();

一切正常。

相关问题