为什么不调用 https.request 回调?



我有一些node.js代码,我试图从raw.github.com获取package.info。我正在执行HTTPS请求,但是由于某种原因,看来从未打过回调,因为"这里"永远不会输出。

有人看到出了什么问题吗?

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

中调用

您需要在请求时调用end(),如以下:

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

最新更新