3

我有一些 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中调用

4

1 回答 1

6

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

req = https.request(options, function(res) {
  console.log('here')
  res.setEncoding('utf8')
  // ... more code here
});
req.end();   // <= Here
于 2012-12-20T03:19:48.780 回答