node.js https 没有响应“结束”事件,而是“关闭”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6537510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:20:18  来源:igfitidea点击:

node.js https no response 'end' event, 'close' instead?

node.jshttps

提问by Nikola Chonkov

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?

我正在使用 node.js v. 0.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', ...),这将来会不会引起任何问题?

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();

采纳答案by Daniel Mendel

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:

我相信这个问题在提交时de09168已经得到修复,几个月来没有对这个问题采取任何行动,但这里是答案:

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

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

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

这不是一个错误。不保证响应具有“结束”事件。使用“关闭”。

Though later he says:

虽然后来他

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

我收回我之前说过的话。我们可能应该使这项工作。

Either way it seems closeand endare pretty much interoperable in this use case. The relevent code in the node.jscore tls.js:681renforces that interpretation:

无论哪种方式,在这个用例中似乎close并且end几乎是可互操作的。node.js核心中的相关代码tls.js:681强化了这种解释:

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

回答by Dario Casta?é

Since node.js 0.8.12 finishevent is emitted on HTTP responses' end function. I wrote all the details in a similar question.

由于 node.js 0.8.12完成事件是在 HTTP 响应的结束函数上发出的。我在一个类似的问题中写了所有细节。