NodeJS + miekal/request:如何中止请求? [英] NodeJS + miekal/request : How to abort a request?
本文介绍了NodeJS + miekal/request:如何中止请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在试图中止请求几个小时,有人可以帮我吗?
I had been trying to abort a request for hours, can anyone help me please?
这就是我所拥有的:
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
).
As you can see its kinda a streaming proxy, so if clients cancels the request I catch it and need to close or abort the forwarding request (theRequest
).
有什么想法吗?
谢谢!
推荐答案
从 https://github.com/mikeal/request/issues/772#issuecomment-32480203 :
从文档中
pipe()返回目标流
pipe() returns the destination stream
,因此您不再使用Request对象.在这种情况下,您要在ServerResponse上调用abort().改为执行此操作:
so you are no longer working with a Request object. In this case you're calling abort() on a ServerResponse. Do this instead:
var theRequest = request({ ... });
theRequest.pipe(parser);
theRequest.abort();
一切正常.
这篇关于NodeJS + miekal/request:如何中止请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文