Skip to content

Instantly share code, notes, and snippets.

@jgwill
Forked from aharshac/Fetch URL title - Node.js
Last active November 6, 2018 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgwill/e5ff88861147c50c2457cc5f5b05250f to your computer and use it in GitHub Desktop.
Save jgwill/e5ff88861147c50c2457cc5f5b05250f to your computer and use it in GitHub Desktop.
Node.js snippet to fetch URL title
/*@@@@
@a Fetch a uniform resource locator title using NodeJS
@d Nov 6, 2018 at 10:10 AM
@tlid 181106091002
@kw
@metatxt Automatic formatting of uniform of the service location when it is posted
@s Working
@CR
@ECR
@CTlid 0
@urir https://gist.github.com/aharshac/4526be3ded1a096819017ab3fca57439
*/
var urlTarget = "https://google.com";
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(urlTarget);
//fetchTitle("https://www.collaborizm.com/");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment