NodeJS中的动态分块响应

时间:2018-05-24 06:49:10

标签: javascript node.js httpresponse rxjs5 multipart

给出以下javascript伪代码(例1),
如您所见,有3 async streams反过来写入响应。他们当然会以异步方式写入响应,因此不保留块的顺序(实际上是不可预测的)。

import pre from './pre';
import content from './content';
import post from './post';

export function renderIndex(req, res) {
  res.writeHead(200, {
    'Content-Type': 'text/html; charset=utf-8',
    'Transfer-Encoding': 'chunked',
  });

  const onEnd = () => {
    if(!pre._readableState.ended) return;
    if(!body._readableState.ended) return;
    if(!post._readableState.ended) return;

    res.end();
  };

  pre.on('data', (chunk) => { res.write(chunk); }).on('end', onEnd);
  body.on('data', (chunk) => { res.write(chunk); }).on('end', onEnd);
  post.on('data', (chunk) => { res.write(chunk); }).on('end', onEnd);
}

是否可以告诉客户端每个数据块的位置?

我想实现这样的目标:

// ---- Stream 1 keep open
<html>
  <head>
  ...
  ...
  ...
  ...


// --- Stream 2 keep open

<body>
  ...
  ...
  ...

// --- Stream 3 keep open
<script src="..."></script>
<script src="..."></script>
<script src="..."></script>


// --- Stream 1 CLOSE
  </head>

// --- Stream 2 CLOSE
  </body>

// --- Stream 3 CLOSE
  </html>


// res.end()
  1. Range Requests
  2. Multipart Range
  3. 类似大理石的说明:

    • 实际[pre] [post] [body] [pre] [body] [/pre] [/post] [/body]
    • 期望 [pre] [/pre] [body] [/body] [post] [/post]

2 个答案:

答案 0 :(得分:1)

我相信您可以通过名为highland.js的库实现预期的行为。它为您提供了一种在流上执行某些操作的方法

/*
the sample to show how it works
H([
  H([1, 2, 3]),
  H([4, 5, 6])
]).sequence().pipe(process.stdout);
*/

import pre from './pre';
import content from './content';
import post from './post';
const H = require('highland')

export function renderIndex(req, res) {
  res.writeHead(200, {
    'Content-Type': 'text/html; charset=utf-8',
    'Transfer-Encoding': 'chunked',
  });

  H([
    pre,
    content,
    post
  ]).sequence().pipe(res);
}

答案 1 :(得分:0)

解决问题的最简单方法是将块写入专用变量和end事件,将整个pre / body / post响应写入res