在Node v10.11中,我试图将对象推向管道,但我总是得到错误.
Run Code Online (Sandbox Code Playgroud)events.js:72 throw er; // Unhandled 'error' event ^ TypeError: Invalid non-string/buffer chunk at validChunk (_stream_writable.js:150:14) at WriteStream.Writable.write (_stream_writable.js:179:12)
我能做到
this.push(chunk)
Run Code Online (Sandbox Code Playgroud)
直接管道数据,但我做不到
var result = {'the web content is': chunk}
this.push(result)
Run Code Online (Sandbox Code Playgroud)
30 LOC中的可运行示例:
var stream = require('stream');
var MsgExtractStream = function() {
stream.Transform.call(this,{objectMode: true});
}
MsgExtractStream.prototype = Object.create(
stream.Transform.prototype, {constructor: {value: MsgExtractStream}} )
MsgExtractStream.prototype._transform = function(chunk, encoding, callback) {
var result = {'the website is': chunk};
this.push(result);
}
MsgExtractStream.prototype.write = function () {
this._transform.apply(this, arguments);
};
MsgExtractStream.prototype.end = function …Run Code Online (Sandbox Code Playgroud) 我在Ubuntu 12.04上使用Node v0.10.11.我无法弄清楚我缺少什么来使URL流与请求模块一起工作.该程序试图访问邮件列表站点,找到每个月的下载链接,然后下载每个月的页面.
Mikael的自述文件说"第一个参数可以是url或options对象.唯一需要的选项是URI,其他所有选项都是可选的.ur || url - 完全限定的uri或url.parse()中解析的url对象"
如果我打电话url.parse(www.targeturl.com),我会
Error: options.uri is a required argument
Run Code Online (Sandbox Code Playgroud)
如果我不使用url.parse,我会
Error: Invalid URI "www.freelists.org/archive/si-list/06-2013"
Run Code Online (Sandbox Code Playgroud)
(此链接在我的浏览器中完美运行)
我把代码减少到了42行.欢迎任何建议
var request = require('request'),
url = require('url'),
stream = require('stream'),
cheerio = require('cheerio'), // a reduced jQuery style DOM library
Transform = require('stream').Transform
var DomStripStream = function(target) {
this.target = target;
stream.Transform.call(this,{objectMode: true});
}
DomStripStream.prototype = Object.create(
Transform.prototype, {constructor: {value: DomStripStream}}
)
DomStripStream.prototype.write = function () {
this._transform.apply(this, arguments);
};
DomStripStream.prototype.end = function () …Run Code Online (Sandbox Code Playgroud) 我对node.js的新streams2 API有点困惑.我尝试创建一个Writable流,但我找不到定义"_end"函数的方法.我只能覆盖"_write"函数.文档中也没有任何内容可以告诉我如何做到这一点.
我正在寻找一种方法来定义一个函数来正确关闭流,之后有人调用mystream.end().
我的流写入另一个流,在关闭我的流之后,我还想在发送所有数据后关闭底层流.
我该怎么做?
它看起来如何:
var stream = require("stream");
function MyStream(basestream){
this.base = basestream;
}
MyStream.prototype = Object.create(stream.Writable);
MyStream.prototype._write = function(chunk,encoding,cb){
this.base.write(chunk,encoding,cb);
}
MyStream.prototype._end = function(cb){
this.base.end(cb);
}
Run Code Online (Sandbox Code Playgroud)