javascript - How to Pipe Response to a File in Co-Request module & NodeJs? - Stack Overflow

I am using Co-Requestto read Zip file from http url, and i have below code to read from server..The c

I am using Co-Request to read Zip file from http url, and i have below code to read from server..

The code works already. But I dont know how to write the response Zip to a file.

var co = require( "co" );
var request = require( "co-request" );
        var options = {
                        url: ".zip",
                        headers: {
                            'Token': Appconfig.Affiliate_Token,
                            'Affiliate-Id' : Appconfig.Affiliate_Id
                        }
                    }
                    console.log( "Downloading : zip file"  );
                    var j = yield request( options );

Co-Request is actually wrapper for Request and I have found below code to pipe file to stream. But not sure how to write the same using Co-Request with yield.

request.get('.png').pipe(request.put('.png'))

Please help how to write response zip to a file using yield and co-request

I am using Co-Request to read Zip file from http url, and i have below code to read from server..

The code works already. But I dont know how to write the response Zip to a file.

var co = require( "co" );
var request = require( "co-request" );
        var options = {
                        url: "http://www.example./sample.zip",
                        headers: {
                            'Token': Appconfig.Affiliate_Token,
                            'Affiliate-Id' : Appconfig.Affiliate_Id
                        }
                    }
                    console.log( "Downloading : zip file"  );
                    var j = yield request( options );

Co-Request is actually wrapper for Request and I have found below code to pipe file to stream. But not sure how to write the same using Co-Request with yield.

request.get('http://example./img.png').pipe(request.put('http://example./img.png'))

Please help how to write response zip to a file using yield and co-request

Share Improve this question edited Apr 19, 2015 at 13:06 Qantas 94 Heavy 16k31 gold badges72 silver badges88 bronze badges asked Apr 12, 2015 at 4:51 amazamaz 4451 gold badge5 silver badges15 bronze badges 2
  • 2 Check out co-request docs: github./request/request/blob/master/README.md You can pipe response to another stream request('google./… – vromanch Commented Apr 15, 2015 at 13:34
  • I don't think you can use generators for piping streams. – Bergi Commented Apr 19, 2015 at 13:36
Add a ment  | 

1 Answer 1

Reset to default 6 +25

I think request cant pipe after data has been emitted from the response

use request instead of co-request, write a promise to achieve this

var co = require('co');
var request = require('request');
var fs = require('fs');

var url = 'http://google./doodle.png';

var requestPipToFile = function(url, filepath) {
    return new Promise(function(resolve, reject) {
        try {
            var stream = fs.createWriteStream(filepath);
            stream.on('finish', function() {
                console.log("pipe finish");
                return resolve(true);
            });
            return request(url).pipe(stream);
        } catch (e) {
            return reject(e);
        }
    });
};

co(function*() {
    var value = (yield requestPipToFile(url, './outfile'));
    return value;
}).then(function(value) {
    return console.log(value);
}).catch(function(err) {
    return console.error(err);
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742337969a4425036.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信