的NodeJS / EX pressJS发送大量数据的响应在1流 [英] NodeJS/ExpressJS send response of large amount of data in 1 stream

查看:95
本文介绍了的NodeJS / EX pressJS发送大量数据的响应在1流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是本地蒙戈REST API是节点返回JSON约400K原型的应用程序。我使用以下方法来MAKET他要求蒙戈的原生API,并返回结果:

  http.request(选项,函数(REQ)
  {
    req.on(数据,功能(数据)
      {
的console.log(数据,data.rows);
        response.send(200,数据);
      }
    );
  }

。对('错误',功能(错误)
  {
的console.log('错误\\ T',错误);
    response.send(500,误差);
  }

。结束();

当我打的http://本地主机:8001 / API / TESTDATA 通过卷曲,响应是正确的(无论是什么输出到节点的控制台从的console.log 什么是卷曲收到)。但是,当我在我的应用程序通过AJAX打它,在流...... interupted,甚至 输出到节点的控制台(终端)数据是奇数:它有多重的EOF和网络>响应在Chrome的开发工具调用在第一EOF结束。

另外一个奇怪的事情:数据如下:

  {
    偏移量:0,
    行:[...]
}

但没有节点,也没有客户端(角),我可以引用data.rows(它返回undefined)。 typeof运算数据收益 [对象的对象]

修改为卷曲和角(所报告的节点)的请求头是:

  req.headers:{
  X-行动':'',
  X-NS':'test.headends',
  内容类型:text / plain的;字符集= utf-8',
  连接:关闭,
  内容长度':'419585'
}

修改我检查响应头两个角和直接(而不是从节点)卷曲,annnd有(从节点从两个卷曲和角度输出相同的,而不是直接的)是一个分歧:

 的访问控制,允许报头:原产地,X-要求-着,内容类型,接受
访问控制允许的方法:OPTIONS,GET,POST,PUT,DELETE
访问控制允许来源:*
连接:保持活动
内容长度:65401//< ----------------太小了!
内容类型:应用程序/八位字节流
// ^ - 如果我强迫应用/ JSON
//使用response.json()而不是response.send()的节点,
//客户端显示八位位组(它需要8S,而不是0的)
日期:星期一,2013年7月15日18时36分50秒GMT
ETAG:-207110537
的x通电方式:前preSS


解决方案

节点的http.request()在块返回数据流媒体(将是很好,如果他们明确说明这一点)。因此,有必要对每块写前preSS的回应身体, 监听HTTP请求结束 (这是不是真的记录),然后调用到Response.End()来真正完成的响应。

  VAR REQ = http.request(选项,函数(RES)
  {
    res.on(数据,功能(块){回复于(块);});
    res.on(结束,函数(){到Response.End();});
  }
);
req.on('错误',功能(错误){...});
req.end();

其中,响应是防爆preSS的反应的初始客户端请求(卷曲或角的Ajax调用)。

I'm prototyping an app using the native mongo rest api where Node returns about 400K of json. I use the following to maket he request to mongo's native api and return the result:

http.request(options, function(req)
  {
    req.on('data', function(data)
      {
console.log(data,data.rows);
        response.send( 200, data );
      }
    );
  }
)
.on('error', function(error)
  {
console.log('error\t',error);
    response.send(500, error);
  }
)
.end();

When I hit http://localhost:8001/api/testdata via curl, the response is proper (both what is outputted to Node's console from the console.log and what is received by curl). But when I hit it via ajax in my app, the stream is…interupted, even data outputted to Node's console (Terminal) is odd: It has multiple EOFs, and the Network > response for the call in chrome's dev tools ends at the first EOF.

One other strange thing: data looks like:

{
    "offset": 0,
    "rows": [ … ]
}

but in neither Node nor client-side (angular) can I reference data.rows (it returns undefined). typeof data returns [object Object].

EDIT The request headers for both curl and angular (as reported by Node) are:

req.headers: {
  'x-action': '',
  'x-ns': 'test.headends',
  'content-type': 'text/plain;charset=utf-8',
  connection: 'close',
  'content-length': '419585'
}

EDIT I checked response headers in both angular and curl directly (instead of from Node), annnd there's a disagreement (same output from both curl and angular directly instead of from node):

access-control-allow-headers: "Origin, X-Requested-With, Content-Type, Accept"
access-control-allow-methods: "OPTIONS,GET,POST,PUT,DELETE"
access-control-allow-origin: "*"
connection: "keep-alive"
content-length: "65401" // <---------------- too small!
content-type: "application/octet-stream"
//             ^-- if i force "application/json"
// with response.json() instead of response.send() in Node,
// the client displays octets (and it takes 8s instead of 0s)
date: "Mon, 15 Jul 2013 18:36:50 GMT"
etag: ""-207110537""
x-powered-by: "Express"

解决方案

Node's http.request() returns data in chunks for streaming (would be nice if they explicitly state this). Thus it's necessary to write each chunk to the body of Express's response, listen for the end of the http request (which is not really documented), and then call response.end() to actually finish the response.

var req = http.request(options, function(res)
  {
    res.on( 'data', function(chunk) { response.write(chunk); } );
    res.on( 'end', function() { response.end(); } );
  }
);
req.on('error', function(error) { … });
req.end();

Where response is Express's response the the initial client request (curl or angular's ajax call).

这篇关于的NodeJS / EX pressJS发送大量数据的响应在1流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆