Skip to content Skip to sidebar Skip to footer

Pipe After Successful Condition Node Js

request.get(fileLink) .on('response', function(response) { if (response.statusCode == 200 && response.headers['content-type'] == 'application/vnd.ms-excel') {

Solution 1:

The way to do it, is to pipe response if the condition is met, and destroy the response stream otherwise.

request.get(fileLink)
    .on('response', function(response) {
        if (response.statusCode == 200 && response.headers['content-type'] == 'application/vnd.ms-excel') {
            return response
                .pipe(fs.createWriteStream('data.xls'))
                .on('error', console.error)
        }

        response.destroy();
    });

Post a Comment for "Pipe After Successful Condition Node Js"