【发布时间】:2011-06-30 15:49:01
【问题描述】:
我正在使用 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', ...) 的替代品吗,这可能会在将来引起任何问题吗?
您可以在下面查看代码示例。 提前致谢!
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();
【问题讨论】:
-
和http一样吗? 'close' 在套接字实际关闭后发出,'end' 在 'http' 请求结束时发出。请求可能有问题,http 解析器认为它没有完整的请求。你能显示完整的请求标头吗?
-
哇,我在这里遇到了同样的问题。正在查看周围的各种 http 示例,发现当我切换到 https 时,它会永远保持打开状态。但是“关闭”修复了它!答案被接受:)。
-
啊,谢谢伙计!上帝保佑你..我在这上面花了好几个小时。