Javascript 使用Node.js中的流缓冲HTTP通信

Javascript 使用Node.js中的流缓冲HTTP通信,javascript,node.js,http,stream,pipe,Javascript,Node.js,Http,Stream,Pipe,我试图使用Node.js中的streams来基本上构建HTTP数据的运行缓冲区,直到完成一些处理,但我正在努力解决streams的细节问题。一些伪代码可能会有所帮助: var server = http.createServer(function(request, response) { // Create a buffer stream to hold data generated by the asynchronous process // to be piped to t

我试图使用Node.js中的streams来基本上构建HTTP数据的运行缓冲区,直到完成一些处理,但我正在努力解决streams的细节问题。一些伪代码可能会有所帮助:

var server = http.createServer(function(request, response) {

    // Create a buffer stream to hold data generated by the asynchronous process
    // to be piped to the response after headers and other obvious response data
    var buffer = new http.ServerResponse();

    // Start the computation of the full response as soon as possible, passing
    // in the buffer stream to hold returned data until headers are written
    beginAsyncProcess(request, buffer);

    // Send headers and other static data while waiting for the full response
    // to be generated by 'beginAsyncProcess'
    sendObviousData(response, function() {

        // Once obvious data is written (unfortunately HTTP and Node.js have
        // certain requirements for the order data must be written in) then pipe
        // the stream with the data from 'beginAsyncProcess' into the response
        buffer.pipe(response);
    });
});
大部分代码几乎都是合法的,但不起作用。基本问题是找到一种方法,当存在与HTTP请求相关的特定顺序要求时,利用Node.js的异步特性,即必须始终首先写入头


虽然我肯定会很感激能有一些小技巧来解决订单问题,而不直接处理流,但我想利用这个机会更好地了解它们。类似的情况有很多,但这种情况比其他任何情况都更容易让蠕虫泛滥。

让我们利用Node.js和
.pause()
/
.resume()
流函数中的回调和流:

var server = http.createServer(function(request, response) {

    // Handle the request first, then..

    var body = new Stream(); // <-- you can implement stream.Duplex for read / write operations
        body.on('open', function(){
            body.pause();
            // API generate data
            // body.write( generated data ) <-- write to the stream
            body.resume();
        });

    var firstPartOfThePage = getHTMLSomeHow();

    response.writeHead(200, { 'Content-Type': 'text/html'});

    response.write(firstPartOfThePage, function(){ // <-- callback after sending first part, our body already being processed
        body.pipe( response ); // <-- This should fire after being resumed
        body.on('end', function(){
            response.end(); // <-- end the response
        });
    });
});
var server=http.createServer(函数(请求、响应){
//先处理请求,然后。。

var body=new Stream();//在这种情况下,实际代码比伪代码要好:)@Urahara基本上就是这样,sendObviousData发送标题、样式表等。这些都是静态的。实际内容是由beginAsyncProcess生成的,并且需要时间来生成。想法是在等待生成内容的同时发送标题、样式表等,然后在准备好后发送。问题是,始终没有他认为,内容可能会超出标题,并导致一个非常讨厌捕获和处理的错误。我正在尝试利用异步代码,同时避免竞争条件。在我看来,您只需在开始beginAsyncProcess()之前发送ObviousData(),但很难用虚构的部分来区分…我经常这样做;立即编写头文件,然后使用异步进程将其导入响应中。end()。除非您希望根据缓慢的异步响应更改标头,否则效果很好。@Environmentalist您听说过
marko
?这允许您在写入响应缓冲区时,通过管道传输传入html模板并异步注入数据。2个想法:1.节点将为您捕获它:2.它似乎是writeHead()都是同步的,所以这不会发生。过程不错,但正如我在问题中所写的(或者更确切地说,我没有在问题中所写)文件系统从来没有任何用途。所有数据都是使用API计算的,没有一个符合Node.js中的streams消费模式。我已经更新了我的答案,你能评论一下吗,它对你有用吗?我认为它绝对值得保留。只要它能工作(我还没有测试过它)它可能会在将来对某人有用。然后+1,我正试图在这里赢得一些尊重;)