i want to prevent loading the rest of the page and just load the -head-, in case the page be too heavy and i just need the -title- so thats a total waste. any idea if this is possible or not?
thank you
>Solution :
Another not-so-clean solution (in addition to Quentin‘s answer) is to "stream" the site to a buffer in chunks and abort once </head> is reached. The problem is, the last chunk could still contain more than just </head>. Should work nonetheless:
const https = require("https");
const options = {
hostname: "stackoverflow.com",
path: "/",
method: "GET",
};
const req = https.request(options, res => {
let buf = Buffer.alloc(0);
res.on("data", chunk => {
buf = Buffer.concat([buf, chunk]);
const headEndIndex = buf.indexOf("</head>");
if (headEndIndex !== -1){
buf = buf.slice(0, headEndIndex + "</head>".length);
console.log(buf.toString());
res.destroy();
}
});
});
req.end();