-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_buffer.js
43 lines (36 loc) · 1.16 KB
/
test_buffer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* Taken by Bob Cochran from this Gist:
* https://gist.github.com/jfsiii/804225
* Complete reworking of JS from https://gist.github.com/803410
* Removes external `request` dependency
* Caveats:
* * No error checking
* * Largely a POC [ed: proof of concept?].
* `data` URI is accurate, but this code cannot simply be inserted
* into an `express` app
*/
var http = require('http')
var options = {
hostname: 'www.nodejs.org',
port: 80,
path: '/logo.png',
method: 'GET'
}
http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
var type = res.headers["content-type"]
var prefix = "data:" + type + ";base64,"
var body = "";
res.on('error', function(e) {
console.log('problem with request: ' + e.message)
})
res.setEncoding('binary');
res.on('end', function () {
var base64 = new Buffer(body, 'binary').toString('base64'),
data = prefix + base64;
console.log(data);
});
res.on('data', function (chunk) {
if (res.statusCode == 200) body += chunk;
});
}).end()