-
-
Save jgwill/e5ff88861147c50c2457cc5f5b05250f to your computer and use it in GitHub Desktop.
Node.js snippet to fetch URL title
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*@@@@ | |
@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