我需要一个将数据发送到字符串的nodejs 流(http://nodejs.org/api/stream.html)实现。你认识什么人吗?
直接地说,我正在尝试通过管道传输请求响应,如下所示:
请求('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'))
FROM https://github.com/mikeal/request https://github.com/mikeal/request
Thanks
编写一个符合Stream接口的类并不困难;这是一个实现非常基础的示例,并且似乎可以与您链接的请求模块一起使用:
var stream = require('stream');
var util = require('util');
var request = require('request');
function StringStream() {
stream.Stream.call(this);
this.writable = true;
this.buffer = "";
};
util.inherits(StringStream, stream.Stream);
StringStream.prototype.write = function(data) {
if (data && data.length)
this.buffer += data.toString();
};
StringStream.prototype.end = function(data) {
this.write(data);
this.emit('end');
};
StringStream.prototype.toString = function() {
return this.buffer;
};
var s = new StringStream();
s.on('end', function() {
console.log(this.toString());
});
request('http://google.com').pipe(s);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)