所以在这段(简化的)代码中,当有人访问我的节点服务器时,我向另一个网站发出GET请求并将HTML页面标题打印到控制台。工作正常:
var http = require("http");
var cheerio = require('cheerio');
var port = 8081;
s = http.createServer(function (req, res) {
var opts = {
method: 'GET',
port: 80,
hostname: "pwoing.com",
path: "/"
};
http.request(opts, function(response) {
console.log("Content-length: ", response.headers['content-length']);
var str = '';
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function() {
dom = cheerio.load(str);
var title = dom('title');
console.log("PAGE TITLE: ",title.html());
});
}).end();
res.end("Done.");
}).listen(port, '127.0.0.1');
然而,在实际应用中,用户可以指定要访问的URL。这意味着我的节点服务器可能会下载20GB的电影文件或其他内容,这并不好。Content-length头也不能阻止这种情况,因为它并未被所有服务器传输。那么问题来了:
我该如何告诉它在接收到前10KB后停止GET请求呢?
谢谢!