Skip to content

Instantly share code, notes, and snippets.

@lxe
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save lxe/1c5ac7430f52e450dfcb to your computer and use it in GitHub Desktop.

Select an option

Save lxe/1c5ac7430f52e450dfcb to your computer and use it in GitHub Desktop.
httpreqstreams.js
// Works (console.logs the headers)
require('http').request('http://www.google.com')
.on('response', function (response) {
console.log(response.headers);
})
.end();
// Doesn't work (hangs)
require('http').request('http://www.google.com')
.on('response', function (response) {
console.log(response.headers)
})
.pipe(require('through2')()) // this should just be a simple passtrhough.... why doesn't it work?
.end();
function logTiming(req) {
var start = Date.now();
req.on('response', function (response) {
// Log response timing for example
console.log(Date.now() - start);
response.resume();
});
return req;
}
logTiming(require('http').request('http://www.google.com'))
.on('response', function (response) {
console.log(response.headers)
})
.end();
var passThroughWithEvents = require('through2')();
passThroughWithEvents.on('pipe', function (req) {
var start = Date.now();
req.on('response', function (response) {
// Log response timing for example
console.log(Date.now() - start);
response.resume();
});
});
require('http').request('http://www.google.com')
.on('response', function (response) {
console.log(response.headers)
})
.pipe(passThroughWithEvents)
.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment