未收到nodejs https响应

时间:2016-01-27 21:13:35

标签: node.js httprequest

我在Windows 7上使用nodejs。我遇到了奇怪的行为。始终提供相同响应的相同https请求大部分时间都未被节点接收,而Web浏览器始终接收该响应。

代码非常简单:

var url1 = 'https:<my url>'
require('request').get(url1).pipe(process.stdout)

响应实际上是一个分块响应,这是一个很大的json对象。

如果我在浏览器上尝试此请求,我总是收到anwser。 如果我使用节点请求模块(我也尝试过使用https),那么主体大部分时间都是空的。但有时它会被退回。

我收到状态200,响应标题为:

{ 'content-type': 'application/json',
  connection: 'close',
  'transfer-encoding': 'chunked',
  date: 'Wed, 27 Jan 2016 21:01:59 GMT',
  server: 'My Server' }

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我找到了2个解决方案来解决这个问题:

1)设置标题'Connection':'keep-alive'

var opts = { url: 'my url', headers :{ 'Connection':'keep-alive'} }
request(opts).pipe(..)

2)在https.globalAgent

中设置为true KeepAlive选项
https.globalAgent.keepAlive=true
相关问题