node.js https 沒有響應“結束”事件,而是“關閉”? (node.js https no response 'end' event, 'close' instead?)


問題描述

node.js https 沒有響應“結束”事件,而是“關閉”? (node.js https no response 'end' event, 'close' instead?)

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

參考解法

方法 1:

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:

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

  

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 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');
});

方法 2:

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

(by Nikola ChonkovDaniel MendelDario Castañé)

參考文件

  1. node.js https no response 'end' event, 'close' instead? (CC BY-SA 3.0/4.0)

#HTTPS #node.js






相關問題

如何信任特定的自簽名證書?(不相信所有人) (How can I trust a specific self-sign certificates? (Not trust all))

使用 Dotnet 客戶端與 Java 服務器進行 Https 通信 (Https Communication to Java Server with Dotnet Client)

在 ssl 站點中啟用 http 小書籤? (Enable http bookmarklet in ssl site?)

HTTPS 客戶端/服務器通信錯誤:底層連接已關閉:發送時發生意外錯誤 (HTTPS Client/Server Communication Error: The underlying connection was closed: An unexpected error occurred on a send)

node.js https 沒有響應“結束”事件,而是“關閉”? (node.js https no response 'end' event, 'close' instead?)

在 IIS 7.5 上運行的 ASP.NET 應用程序上強制使用 Https (Force Https on an ASP.NET app running on IIS 7.5)

通過http實現安全性的最佳方式是什麼 (What's the best way to implement security through http)

如何使用身份驗證 API 觸發密碼重置流程、python 或 curl (How to use the authentication API to trigger password reset flow, python or curl)

C#:強制 TLS1.2 但 Https 服務器仍然給出常見算法錯誤 (C#: Forcing TLS1.2 but Https server still giving common algorithm error)

curl 在我的共享主機上無法正常工作 (curl not working in the right way on my shared hosting)

如何為通過 Kestrel 服務器託管 PWA 的 UWP 桌面橋應用配置 https (How to configure https for a UWP Desktop Bridge app hosting a PWA via a Kestrel Server)

從其他機器訪問 webpack DevServer 子 URL 時出現 ERR_SSL_PROTOCOL_ERROR (ERR_SSL_PROTOCOL_ERROR when accessing webpack DevServer sub-URLs from a different machine)







留言討論