2012-12-20 59 views
2

我有一些node.js代码,我试图从raw.github.com获取package.info。我正在做一个HTTPS请求,但由于某种原因,它看起来像回调永远不会被调用,因为'here'永远不会被输出。为什么不调用https.request回调?

有没有人看到发生了什么问题?

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

你需要调用end()的要求来执行它,就像这样:

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