Merge pull request #148 from jimmywarting/patch-2

update readme with binary file/buffer examples
This commit is contained in:
David Frank 2016-08-16 19:27:37 +08:00 committed by GitHub
commit 8fc30a8d8c
1 changed files with 18 additions and 1 deletions

View File

@ -64,7 +64,24 @@ fetch('https://api.github.com/users/github')
console.log(json);
});
// since v1.6.0, there is also res.buffer() for convenience
// Binary
// The perfered way is to use the stream's
// when working with larger binaries
fetch('some.png')
.then(function(res) {
var dest = fs.createWriteStream('./img/some.png');
res.body.pipe(dest);
})
// When working with smaller binaries like
// with protobuf it can be good to use .buffer()
fetch('http://example.com/api/proto')
.then(function(res) {
return res.buffer();
}).then(function(buffer) {
console.log(buffer);
});
// meta