Eu estava tentando abortar um pedido por horas, alguém pode me ajudar, por favor?
Isso é o que eu tenho:
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
});
});
Como você pode ver, é meio que um proxy de streaming, então se o cliente cancelar a solicitação, eu o pego e preciso fechar ou abortar a solicitação de encaminhamento ( theRequest
).
Alguma ideia?
Obrigado!
var r = request(url)
r.abort()
Devemos colocar isso na documentação.
Obrigado pela resposta @mikeal , eu tentei isso:
var theRequest = request({
'method' : 'GET',
'url': '/anotherUrl',
'headers' : {
"ACCEPT" : "text/event-stream"
}
}, function (error, response, body){
if (error) {
//Handle error
}
}).pipe(parser);
theRequest.abort();
E esse erro levanta:
/home/vagrant/gateway/app.js:876
theRequest.abort();
^
TypeError: Object #<ServerResponse> has no method 'abort'
at IncomingMessage.<anonymous> (/home/vagrant/gateway/app.js:876:18)
at IncomingMessage.EventEmitter.emit (events.js:92:17)
at abortIncoming (http.js:1912:11)
at Socket.socket.onend (http.js:2010:7)
at Socket.g (events.js:180:16)
at Socket.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
Você poderia me ajudar por favor?
Obrigado!
Dos documentos:
pipe()
retorna o fluxo de destino
então você não está mais trabalhando com um objeto Request
. Neste caso, você está chamando abort()
em um ServerResponse
. Em vez disso, faça isso:
var theRequest = request({ ... });
theRequest.pipe(parser);
theRequest.abort();
Muito obrigado!
Muito obrigado!
Comentários muito úteis
Devemos colocar isso na documentação.