Last active
August 29, 2015 14:11
-
-
Save lxe/1c5ac7430f52e450dfcb to your computer and use it in GitHub Desktop.
httpreqstreams.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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