Skip to content

Instantly share code, notes, and snippets.

@aharshac
Last active February 25, 2021 06:47
Show Gist options
  • Save aharshac/4526be3ded1a096819017ab3fca57439 to your computer and use it in GitHub Desktop.
Save aharshac/4526be3ded1a096819017ab3fca57439 to your computer and use it in GitHub Desktop.
Node.js snippet to fetch URL title
var request = require('request');
var cheerio = require('cheerio');
function fetchTitle(url, onComplete = null) {
request(url, function (error, response, body) {
var output = url; // default to URL
if (!error && response.statusCode === 200) {
var $ = cheerio.load(body);
console.log(`URL = ${url}`);
var title = $("head > title").text().trim();
console.log(`Title = ${title}`);
output = `[${title}](${url})`;
} else {
console.log(`Error = ${error}, code = ${response.statusCode}`);
}
console.log(`output = ${output} \n\n`);
if (onComplete) onComplete(output);
});
}
fetchTitle("https://github.com/aharshac/KandyCpp");
fetchTitle("https://www.collaborizm.com/");
@MXP2095onetechguy
Copy link

this is using a deprecated module, Step Up your game and use an alternative of request, but maybe add it to a new file in the same gist, keep this one for archival

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment