대부분의 일을 위해, 당신은, 당신이 all the work I do with streams를 들어 당신
에 대한 모든 설정 그냥 아주 편리한 래퍼 것을 볼 수 있습니다, 당신은 the source code for the Stream.prototype.pipe implementation 보면
src.pipe(dest);
을 사용할 수 있어야합니다 일반적으로 적절한 스트림 유형 (Readable
, Writable
, Duplex
, Transform
또는 PassThrough
)을 선택한 다음 적절한 방법 (_read
, _write
및/또는)을 정의합니다.). 마지막으로 .pipe
을 사용하여 모든 것을 연결합니다.
그것은 여기 내 burro 모듈에 사용하고 스트림의, 예를 들어
client.pipe(encoder).pipe(server).pipe(decoder).pipe(client)
"원형"으로 표시 스트림 설정을 보는 것은 매우 일반적입니다. 개체를이 스트림에 쓸 수 있으며 JSON 문자열을 읽을 수 있습니다.
// https://github.com/naomik/burro/blob/master/lib/encoder.js
var stream = require("stream"),
util = require("util");
var Encoder = module.exports = function Encoder() {
stream.Transform.call(this, {objectMode: true});
};
util.inherits(Encoder, stream.Transform);
Encoder.prototype._transform = function _transform(obj, encoding, callback) {
this.push(JSON.stringify(obj));
callback(null);
};
일반적인 권장 사항으로, 거의 항상 이처럼 스트림을 작성합니다. 즉, 내장 된 스트림 중 하나에서 상속받은 자신 만의 "클래스"를 작성합니다. 내장 스트림을 직접 사용하는 것은 실제적이지 않습니다.
당신이 이것을 사용
var encoder = new Encoder();
가 배관에 의해 인코더 출력
encoder.pipe(process.stdout);
일부를 쓰기 stdout에 무엇을 참조하십시오 스트림의 새로운 인스턴스를 생성하여 시작하는 방법을 보여주기 위해 샘플 객체
encoder.write({foo: "bar", a: "b"});
// '{"foo":"bar","a":"b"}'
encoder.write({hello: "world"});
// '{"hello":"world"}'
아마도 https://www.youtube.com/watch?v=QgEuZ52OZtU가 도움이됩니다. :-) –
Golo Roden에게 고맙습니다. 나는 "시간이 지남에 따라 배열"을 좋아했습니다 :) –