Node.js HTTP response streams

dzm

Using the native http.get() in Node.js, I'm trying to pipe a HTTP response to a stream that I can bind data and end events to.

I'm currently handling this for gzip data, using:

http.get(url, function(res) {
  if (res.headers['content-encoding'] == 'gzip') {
    res.pipe(gunzip);
    gunzip.on('data', dataCallback);
    gunzip.on('end', endCallback);
  }
});

Gunzip is a stream and this just works. I've tried to create streams (write streams, then read streams) and pipe the response, but haven't been having much luck. Any suggestions to replicate this same deal, for non-gzipped content?

hexacyanide

The response object from a HTTP request is an instance of readable stream. Therefore, you would collect the data with the data event, then use it when the end event fires.

var http = require('http');
var body = '';

http.get(url, function(res) {
  res.on('data', function(chunk) {
    body += chunk;
  });
  res.on('end', function() {
    // all data has been downloaded
  });
});

The readable.pipe(dest) would basically do the same thing, if body in the example above were a writable stream.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

http模块上的node.js response.writeHead

来自分类Dev

http模块上的node.js response.writeHead

来自分类Dev

Node.js中的Streams3是什么,它与Streams2有何不同?

来自分类Dev

Node.js中的Streams3是什么,它与Streams2有何不同?

来自分类Dev

Node JS TCP代理:使用Node JS设置HTTP隧道

来自分类Dev

Node JS TCP代理:使用Node JS设置HTTP隧道

来自分类Dev

Node.js HTTP服务器

来自分类Dev

Express (node.js) using HTTPS and HTTP

来自分类Dev

Node.js HTTP响应流

来自分类Dev

监视Node.js HTTP Server的命令

来自分类Dev

Node.js HTTP状态代码列表

来自分类Dev

Node JS上的HTTP请求回调

来自分类Dev

Node.js axios http请求循环

来自分类Dev

Node.js HTTP服务器

来自分类Dev

node.js http代理重定向

来自分类Dev

node.js 中的 Http.request

来自分类Dev

Node js http 请求:选项中的变量

来自分类Dev

Node.js Http 写入结束后

来自分类Dev

node.js http模块http.createServer如何工作?

来自分类Dev

Node.js截取Promise并精心设计Response

来自分类Dev

Node.js:https.request的response.write()吗?

来自分类Dev

response.on是什么意思?Node.js

来自分类Dev

ERR_EMPTY_RESPONSE /无法获取node.js

来自分类Dev

Node.js截取Promise并精心设计Response

来自分类Dev

node.js 中的“response.write”方法是否超时?

来自分类Dev

Node.js 的代理请求:net::ERR_EMPTY_RESPONSE

来自分类Dev

Node js中response.send和response.write之间的区别

来自分类Dev

将媒体放入Node.js上的Kinesis Video Streams中

来自分类Dev

Node.js - Express.js JWT always returns an invalid token error in browser response