Skip to content

Instantly share code, notes, and snippets.

@watson
Last active January 1, 2016 04:48
Show Gist options
  • Select an option

  • Save watson/8093917 to your computer and use it in GitHub Desktop.

Select an option

Save watson/8093917 to your computer and use it in GitHub Desktop.
API brainstorm for treating node.js http(s) as a stream
var http2 = require('http2'); // working title
http2.get('http://foo.com').pipe(...);
http2('http://foo.com').pipe(...);
http2('https://example.com')
.on('response', function (res) {
// e.g. allows you to inspect the headers and take appropriate action
if (res.headers['some-header'] === 'some-value')
this.abort();
})
.on('data', function (chunk) {
// we are getting data... deal with it!
})
.on('end', function () {
// end of transmission
})
.on('body', function (body) {
// this is the entire body - easier if you don't require streaming
})
.on('error', function (err) {
// this could either be a request or a response error
});
var req = http2('http://foo.com');
req.setHeader('foo', 'bar');
var chunk = req.read();
var req = http2.post('http://foo.com');
someDateStream.pipe(req).on('data', function (chunk) {
// we got data back
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment