Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Nodejs, Is there anyway to load only the head part of a Html url?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading