Reputation: 785
I'm creating a request using the request npm module. However in case the content type is not json, i want the stream to stop. At the moment, it does not stop and and error is generated because unzip reads an invalid header.. How can we stop a stream?
var req = request(options)
req
.on('error', function (err) { console.log("err"); reject(err) })
.on('response', function(response) {
if(response.headers['content-type'] != 'application/json')
{
console.log(response.headers['content-type'])
req.destroy() // doesn't work
this.destroy() // doesn't work
}
})
.on('end', function () { resolve() })
.pipe(gunzip) // ERROR!
Upvotes: 3
Views: 5784
Reputation: 1080
you can use stream.pause() as follows:
var req = request(options)
req
.on('error', function (err) { console.log("err"); reject(err) })
.on('response', function(response) {
if(response.headers['content-type'] != 'application/json')
{
console.log(response.headers['content-type'])
req.pause(); // stream paused
reject('not json');
}
})
.on('end', function () { resolve() })
.pipe(gunzip)
Upvotes: 5