简体   繁体   English

在Node.js中使用流来缓冲HTTP通信

[英]Using streams in Node.js to buffer HTTP communications

I'm trying to use streams in Node.js to basically build a running buffer of HTTP data until some processing is done, but I'm struggling with the specifics of streams. 我正在尝试使用Node.js中的流来基本构建HTTP数据的运行缓冲区,直到完成一些处理为止,但是我在流的细节方面苦苦挣扎。 Some pseudocode will probably help: 一些伪代码可能会有所帮助:

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);
    });
});

Most of this is almost legitimate code, but it doesn't work. 其中大多数是几乎合法的代码,但是不起作用。 The basic issue is figuring out a way to take advantage of the asynchronous nature of Node.js when there are certain order requirements associated with HTTP requests, namely that headers must always be written first. 基本问题是在与HTTP请求相关联的某些顺序要求时,即想出一种方法,以利用Node.js的异步特性,即必须始终首先编写标头。

While I would definitely appreciate any answers with little hacks to get around the order problem without directly addressing streams, I wanted to use the opportunity to get to know them better. 尽管我肯定会喜欢用很少的技巧来解决订单问题而无需直接解决流问题的任何答案,但我想借此机会更好地了解它们。 There are plenty of similar situations, but this scenario is more to open the can of worms than anything else. 有很多类似的情况,但是这种情况比其他任何事情更容易打开蠕虫的罐头。

Let's make a use of callbacks and streams in Node.js and .pause() / .resume() stream functions: 让我们在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
        });
    });
});

Check this: http://codewinds.com/blog/2013-08-31-nodejs-duplex-streams.html for costum duplex stream creation. 请检查以下内容: http ://codewinds.com/blog/2013-08-31-nodejs-duplex-streams.html以创建Costum双工流。

Note: it's still a pseudo code 注意:它仍然是伪代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM