From d8c696973633a870f0aae0f7dc288cca967091db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Karl=20Roland=20W=C3=A4rting?= Date: Tue, 16 Aug 2016 11:40:31 +0200 Subject: [PATCH] added 2 binary exemple Got to many "how do i handle binary/images" issues --- README.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8f55e21..a199a6c 100644 --- a/README.md +++ b/README.md @@ -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