Skip to content

Instantly share code, notes, and snippets.

@scripting
Last active April 2, 2018 15:36
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 scripting/c12569ed806744f0c6f531cbd4c02f21 to your computer and use it in GitHub Desktop.
Save scripting/c12569ed806744f0c6f531cbd4c02f21 to your computer and use it in GitHub Desktop.
A simple Node function to dereference a URL, to find out what it points to through redirects.
const request = require ("request");
function derefUrl (url, callback) {
var theRequest = {
method: "HEAD",
url: url,
followAllRedirects: true
};
request (theRequest, function (err, response) {
if (err) {
callback (err);
}
else {
callback (undefined, response.request.href);
}
});
}
derefUrl ("http://www.scripting.com/rss.xml", function (err, urlDeref) {
console.log (urlDeref);
});
@timbotron
Copy link

Very cool!

I wrote something similar in PHP a while ago. (99% just cURL) https://tim.hithlonde.com/2016/get-final-url-after-redirects/

Now I kind of want a library of just this kind of function in all the various languages out there. :)

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