【问题标题】:Why isn't the https.request callback called?为什么不调用 https.request 回调?
【发布时间】:2012-12-20 03:08:58
【问题描述】:

我有一些 node.js 代码,我试图从 raw.github.com 获取 package.info。我正在做一个 HTTPS 请求,但由于某种原因,它看起来从未调用过回调,因为从未输出过 'here'。

有人知道出了什么问题吗?

console.log(options)

req = https.request(options, function(res) {
  console.log('here')
  res.setEncoding('utf8')
  // ... more code here
 })

 console.log(req)

 // .. return -> listening and waiting

输出

{ host: 'raw.github.com',
  port: 443,
  path: '/jasny/bootstrap/2.2.2-j3-wip/package.json',
  method: 'GET' }
{ domain: null,
  _events:
   { response: { [Function: g] listener: [Function] },
     socket: { [Function: g] listener: [Function] } },
  _maxListeners: 10,
  output: [],
  outputEncodings: [],
  writable: true,
  _last: false,
  chunkedEncoding: false,
  shouldKeepAlive: true,
  useChunkedEncodingByDefault: false,
  sendDate: false,
  _hasBody: true,
  _trailer: '',
  finished: false,
  agent:
   { domain: null,
     _events: { free: [Function] },
     _maxListeners: 10,
     options: {},
     requests: {},
     sockets: { 'raw.github.com:443': [Object] },
     maxSockets: 5,
     createConnection: [Function: createConnection] },
  socketPath: undefined,
  method: 'GET',
  path: '/jasny/bootstrap/2.2.2-j3-wip/package.json',
  _headers: { host: 'raw.github.com' },
  _headerNames: { host: 'Host' }
}

完整代码见lib/packageinfo.js。该函数在index.js中调用

【问题讨论】:

    标签: node.js twitter-bootstrap https jasny-bootstrap


    【解决方案1】:

    您需要在请求上调用end() 来执行它,如下所示:

    req = https.request(options, function(res) {
      console.log('here')
      res.setEncoding('utf8')
      // ... more code here
    });
    req.end();   // <= Here
    

    【讨论】:

      猜你喜欢
      • 2018-07-31
      • 2023-03-08
      • 2015-03-09
      • 1970-01-01
      • 2018-11-12
      • 2018-01-03
      • 2020-08-12
      • 2021-06-18
      • 2014-11-05
      相关资源
      最近更新 更多