node.js https没有响应'结束'事件,'关闭'而不是? [英] node.js https no response 'end' event, 'close' instead?

查看:131
本文介绍了node.js https没有响应'结束'事件,'关闭'而不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用node.js v.4.4.8。当我创建一个HTTPS服务器时,我从来没有得到response.on('end',...)事件(但我做的是HTTP事件)。我在节点的github页面上阅读了问题报告 - https://github.com/joyent/node/issues / 728 ,显然这是一个回归到0.4.8的问题。 response.on('close',...)似乎具有相同的功能,但我不太了解HTTP / HTTPS来判断。我可以用它作为response.on('end',...)的替代品,这可能会在将来引起任何问题吗?

I am using node.js v. 0.4.8. When I create a HTTPS server, I never get the response.on('end', ...) event (but I do for a HTTP one). I read the issue reports on node's github page - https://github.com/joyent/node/issues/728, and apparently this is an issue that regressed into 0.4.8. response.on('close', ...) seems to have the same functionality, but I do not know enough about HTTP/HTTPS to judge. Can I use it as replacement for response.on('end', ...), and is this likely to cause any problems in future?

您可以在下面看到代码示例。
提前致谢!

You can see a code sample below. Thanks in advance!

var request = "JSON_request";
var https = require('https');
var options = { 
        host:"someHost.com",
        path:"somePath",
        method: "POST", 
        headers: {
            'Content-Type': 'application/json',
            'Content-Length': Buffer.byteLength(request)
        }
    };

var req = https.request(options, function(res){
    var response = "";
    res.setEncoding('utf8');

    res.on('data', function(chunk){
        console.log("INFO: "+chunk);
        response += chunk;
    });

    // This never happens
    res.on('end', function(){
        console.log("End received!");
    });

    // But this does
    res.on('close', function(){
        console.log("Close received!");
    });
});

req.on('error', function(error){
    console.log("Error: "+error);
});

req.write(request);
req.end();


推荐答案

我认为这个问题已经修复为提交 de09168 ,并且没有对此进行任何操作几个月的问题,但这里是答案:

I believe this issue has been fixed as of commit de09168, and there hasn't been any action on this question for months BUT here is the answer:

关于的评论问题728 Node.js创建者Ryan Dahl

On the comments for issue 728 Node.js creator Ryan Dahl says:


这不是错误。回复不保证会有结束事件。使用'close'。

This isn't a bug. Responses are not guaranteed to have an 'end' event. Use 'close'.

虽然后来他


我收回我之前所说的内容。我们应该可以做到这一点。

i take back what i said before. we should probably make this work.

无论哪种方式,似乎关闭和<在这个用例中,code> end 几乎可以互操作。 node.js core tls.js:681 中的相关代码重新强制执行该解释:

Either way it seems close and end are pretty much interoperable in this use case. The relevent code in the node.js core tls.js:681 renforces that interpretation:

process.nextTick(function() {
  self.encrypted.emit('end');
  self.cleartext.emit('end');
  self.encrypted.emit('close');
  self.cleartext.emit('close');
});

这篇关于node.js https没有响应'结束'事件,'关闭'而不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆