条件条件成功后管道 [英] Pipe after successful condition Node js

查看:84
本文介绍了条件条件成功后管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

如果响应代码为200并且内容类型为application / vnd.ms-,我需要保存文件优秀。

I need to save file if response code is 200 and content-type is application/vnd.ms-excel. How to organize code?

推荐答案

做到这一点的方法是通过管道传递响应如果满足条件,则销毁响应流。

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();
    });

这篇关于条件条件成功后管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆