Node.Js通过2修改stream

我正在尝试使用通过2的Node.jsstream。 我有一个stream,这是一个index.html文件,我试图修改index.html内容行,每行添加一个------- 。 我有:

  indexHTML .pipe(through2.obj(function(obj, enc, next) { blah = obj.contents.toString().split('\n'); blah.forEach(function(element) { this.push("-------"); this.push(element): }); next(); })).pipe(process.stdout); 

我现在遇到的问题this.push()blah.forEach()数组方法中不可用。 有关如何实现修改index.htmlstream的任何build议?

如果你想保持forEach()而不是常规的for循环, forEach()接受第二个参数 ,它是所需的上下文:

 blah.forEach(function(element) { this.push("-------"); this.push(element): }, this); 

您也可以始终使用更一般的“自我”解决方法:

 var self = this; blah.forEach(function(element) { self.push("-------"); self.push(element): });