2013-10-18 22 views
21

有沒有NodeJS'passthrough'流?是否有NodeJS'passthrough'流?

即一個對象,無論我放入它立即出來,不變。

這似乎毫無意義,但它在開發過程中作爲快速變化代碼的「靜態中心」是有用的。

+4

[也許](http://nodejs.org/api/stream.html#stream_class_stream_passthrough)(0.10+)。 –

+1

@JonathanLonowski謝謝,我讀過那個頁面20次,仍然錯過了。讓它成爲答案,我會接受它。 – fadedbee

回答

2

當您需要將TCP服務器的輸入字節發送到另一個TCP服務器時,它真的很方便。

在我microntoller應用程序的Web部件我使用這個如下

var net = require('net'), 
     PassThroughStream = require('stream').PassThrough, 
     stream = new PassThroughStream(); 

    net.createServer({allowHalfOpen: true}, function(socket) { 
    socket.write("Hello client!"); 
    console.log('Connected:' + socket.remoteAddress + ':' + socket.remotePort); 
    socket.pipe(stream, {end: false}); 
    }).listen(8080); 

    net.createServer(function(socket) { 
    stream.on('data', function (d) { 
     d+=''; 
     socket.write(Date() + ':' + ' ' + d.toUpperCase()); 
    }); 
    socket.pipe(stream); 
    }).listen(8081); 
+0

出於好奇,如果它們必須共享相同的流引用,爲什麼將字節從一個TCP服務器傳遞到下一個服務器會有用? –