因此,在这段(简化的)代码中,当有人访问我的节点服务器时,我向另一个网站发出 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 请求?
干杯!