为什么不调用https.request回调?(Why isn't the https.request callback called?)

编程入门 行业动态 更新时间:2024-10-19 23:34:39
为什么不调用https.request回调?(Why isn't the https.request callback called?)

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

I have some node.js code where I'm trying to get package.info from raw.github.com. I'm doing a HTTPS request, but for some reason it looks like the callback is never called, because 'here' is never outputted.

Does anyone see what's going wrong?

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

Output

{ 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' } }

For the full code see lib/packageinfo.js. The function is called in index.js

最满意答案

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

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

You need to call end() on the request to execute it, like this:

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

更多推荐

本文发布于:2023-04-27 23:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329702.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:回调   request   https   callback   called

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!