Thursday, 16 March 2017

Node.js: How to stop a pipe from streaming data

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 do this?

    request(options)
    .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
        }
    })
    .on('end', function () { resolve() })

    .pipe(gunzip)  // ERROR!



via Captain Obvious

No comments:

Post a Comment